以下程序适用于Windows,但不适用于Linux。当我按下我绑定的键时,没有显示打印语句。但是,Tab键可以在两个按钮之间切换,enter和space键可以切换它们。没有其他钥匙可以工作。
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()
答案 0 :(得分:1)
而不是使用EVT_CHAR_HOOK使用EVT_KEY_DOWN。
答案 1 :(得分:1)
或者确实,EVT_KEY_UP
它解决了按键给出多个按键事件的问题。