0

The following program works for me in Windows but not in Linux. There are no print statements displayed when I press the keys that I binded. However, the tab key can switch between the two buttons and the enter and space key toggle them. No other keys work.

import wx

class MyForm(wx.Frame):
    def __init__(self):
            wx.Frame.__init__(self, None, wx.ID_ANY, "Pressing dem keyz")

            # Add a panel so it looks the correct on all platforms
            panel = wx.Panel(self, wx.ID_ANY)
            self.btn = wx.ToggleButton(panel, label="TOGGLE")
            self.btn2 = wx.ToggleButton(panel, label="TOGGLE 2", pos = (85,0))

            self.btn.Bind(wx.EVT_CHAR_HOOK, self.onKeyPress)
            self.btn2.Bind(wx.EVT_CHAR_HOOK, self.onKeyPress)

    def onKeyPress(self, event):
            space = False
            keycode = event.GetKeyCode()
            print keycode
            if keycode == wx.WXK_SPACE: 
                    print "SPACEBAR!"
                    space = True
            self.btn.SetValue(space)
            if space == True:
                    print "Do something"
            elif keycode == wx.WXK_RETURN:
                    self.Hello()
            elif keycode == wx.WXK_LEFT:
                    self.btn2
                    print 'YOU MOVED LEFT'
            elif keycode == wx.WXK_RIGHT:
                    self.btn
                    print 'YOU MOVED RIGHT'
            elif keycode == wx.WXK_UP:
                    print 'YOU MOVED UP'
            elif keycode == wx.WXK_DOWN:
                    print 'YOU MOVED DOWN'
            elif keycode == wx.WXK_ESCAPE:
                    self.Destroy()
    def Hello(self):
            print "Hello"
            return
 # Run the program
if __name__ == "__main__":
        app = wx.App(False)
        frame = MyForm()
        frame.Show()
        app.MainLoop()
Yello Four
  • 227
  • 1
  • 4
  • 17

2 Answers2

1

Instead of using EVT_CHAR_HOOK use EVT_KEY_DOWN.

Yello Four
  • 227
  • 1
  • 4
  • 17
1

Or indeed, EVT_KEY_UP
which gets rid of the problem of a key held down giving multiple key down events.

Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60