question : wx.WXK_ADD, wx.WXK_NUMPAD_ADD, wx.WXK_INSERT

Dear All ...
Below is part of my code
----START------
     def ListEvtKeyDown(self, event): # wxGlade: MyFrame1.<event_handler>
  global ListEdit
  keycode = event.GetKeyCode()
  #print keycode
  if keycode in (wx.WXK_ADD, wx.WXK_NUMPAD_ADD, wx.WXK_INSERT):
    print "Add List Item"
          self.text_ctrl_1.Enable(True)
    ListEdit = 0
          self.text_ctrl_1.SetFocus()
  event.Skip()

-----STOP------

When I try to push "Insert" key on my keyboard, the script work as expected.

But when I try same thing with "+" and "numeric keypad +" , it didn't do anything.

Is it a bug or I did something wrong ?

Sincerely
-bino-

Hi Bino,

Dear All ...
Below is part of my code
----START------
def ListEvtKeyDown(self, event): # wxGlade: MyFrame1.<event_handler>
global ListEdit
keycode = event.GetKeyCode()
#print keycode
if keycode in (wx.WXK_ADD, wx.WXK_NUMPAD_ADD, wx.WXK_INSERT):
print "Add List Item"
self.text_ctrl_1.Enable(True)
ListEdit = 0
self.text_ctrl_1.SetFocus()
event.Skip()

-----STOP------

When I try to push "Insert" key on my keyboard, the script work as expected.

But when I try same thing with "+" and "numeric keypad +" , it didn't do
anything.

Is it a bug or I did something wrong ?

Sincerely
-bino-

I got the following code to work with two of the keys (Insert and
numpad's addition key)

<code>
import wx

class MyForm(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial",
size=(500,500))

        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)

        panel.Bind(wx.EVT_KEY_DOWN, self.onKeyPress)

···

On Mar 3, 3:54 am, Bino Oetomo <b...@hkb.co.id> wrote:

#----------------------------------------------------------------------
    def onKeyPress(self, event):
        """"""
        keycode = event.GetKeyCode()
        print keycode
        if keycode in (wx.WXK_ADD, wx.WXK_NUMPAD_ADD, wx.WXK_INSERT):
            print "key code found!"

# Run the program
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyForm().Show()
    app.MainLoop()
</code>

The problem with the normal "+" sign is that you have to press "shift"
first to get it on most keyboards. I think there's some way to get at
that using char events, but it's complex and annoying so I recommend
using a wx.AcceleratorTable instead for key combos.

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

Dear Mike

I really appreciate your enlightment

Hi Bino,

I got the following code to work with two of the keys (Insert and
numpad's addition key)

<code>
import wx

class MyForm(wx.Frame):

def \_\_init\_\_\(self\):
    wx\.Frame\.\_\_init\_\_\(self, None, wx\.ID\_ANY, &quot;Tutorial&quot;,

size=(500,500))

    \# Add a panel so it looks the correct on all platforms
    panel = wx\.Panel\(self, wx\.ID\_ANY\)

    panel\.Bind\(wx\.EVT\_KEY\_DOWN, self\.onKeyPress\)

#----------------------------------------------------------------------
def onKeyPress(self, event):
""""""
keycode = event.GetKeyCode()
print keycode
if keycode in (wx.WXK_ADD, wx.WXK_NUMPAD_ADD, wx.WXK_INSERT):
print "key code found!"

# Run the program
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MyForm().Show()
app.MainLoop()
</code>

The problem with the normal "+" sign is that you have to press "shift"
first to get it on most keyboards. I think there's some way to get at
that using char events, but it's complex and annoying so I recommend
using a wx.AcceleratorTable instead for key combos.

-------------------
Mike Driscoll

I copy your code and run it
Work like a champ.

I still got problem with wx.WXK_NUMPAD_ADD

Below is my code, generated by wxGlade
----START------
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# generated by wxGlade 0.6.3 on Thu Mar 4 08:35:40 2010

import wx

# begin wxGlade: extracode
# end wxGlade

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.list_ctrl_1 = wx.ListCtrl(self, -1, style=wx.LC_REPORT|
wx.SUNKEN_BORDER)

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_LIST_KEY_DOWN, self.OnKeyPres,
self.list_ctrl_1)
        # end wxGlade

  self.list_ctrl_1.InsertColumn(0,"Code")

  self.list_ctrl_1.InsertStringItem(self.list_ctrl_1.GetItemCount()+1,
"One")
  self.list_ctrl_1.InsertStringItem(self.list_ctrl_1.GetItemCount()+1,
"Two")
  self.list_ctrl_1.InsertStringItem(self.list_ctrl_1.GetItemCount()+1,
"Three")

    def __set_properties(self):
        # begin wxGlade: MyFrame.__set_properties
        self.SetTitle("frame_1")
        self.list_ctrl_1.SetFocus()
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyFrame.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_1.Add(self.list_ctrl_1, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()
        # end wxGlade

    def OnKeyPres(self, event): # wxGlade: MyFrame.<event_handler>
        print "Key pres event on list Widget : " +
str(event.GetKeyCode())
        if event.GetKeyCode() in (wx.WXK_NUMPAD_ADD, wx.WXK_INSERT):
            print "------That Key is Numpad_Add or Insert"
        event.Skip()

# end of class MyFrame

if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frame_1 = MyFrame(None, -1, "")
    app.SetTopWindow(frame_1)
    frame_1.Show()
    app.MainLoop()
----STOP-------

The result is :
1. The first "print" statement always called, no matter what key
presed
2. The 2nd "print" only called when I press "insert" key, it is not
called when I pres the "+" button of NumPad.

I Did a small experiment :
----STOP-------
bino@bino:~/wxwidget/lat03$ python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.

import wx
print wx.WXK_NUMPAD_ADD

392

----START------

From that experiment I found that :
1. wx.WXK_NUMPAD_ADD have a value of 392, while
2. event.GetKeyCode() of my script repoting a pres of "+" key as : 43

Is it a bug or I did something wrong ?

Sincerely
-bino-

···

On Mar 3, 10:56 pm, Mike Driscoll <kyoso...@gmail.com> wrote:

What is the value of keycode when you press the keys? You can also use the KeyEvents sample in the demo to learn more about what to expect.

···

On 3/3/10 1:54 AM, Bino Oetomo wrote:

Dear All ...
Below is part of my code
----START------
def ListEvtKeyDown(self, event): # wxGlade: MyFrame1.<event_handler>
global ListEdit
keycode = event.GetKeyCode()
#print keycode
if keycode in (wx.WXK_ADD, wx.WXK_NUMPAD_ADD, wx.WXK_INSERT):
print "Add List Item"
self.text_ctrl_1.Enable(True)
ListEdit = 0
self.text_ctrl_1.SetFocus()
event.Skip()

-----STOP------

When I try to push "Insert" key on my keyboard, the script work as
expected.

But when I try same thing with "+" and "numeric keypad +" , it didn't do
anything.

--
Robin Dunn
Software Craftsman