我无法捕获EVT_KEY_DOWN事件。但是可以捕获EVT_KEY_UP
Python版本: 2.7.3
wxPython版本: 2.8.12.1(gtk2-unicode)
系统信息: Mint 14 Nadia,Linux 3.5.0-17-generic(x86_64)
以下是我的代码
#!/usr/bin/env python
import sys, os
import wx
class WinFrame(wx.Frame):
def __init__(self, parent, title):
super(WinFrame, self).__init__(parent,
title=title,
size=(400,400))
self.panel = wx.Panel(self,-1, size=(400,400))
self.panel.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
self.panel.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
def OnKeyUp(self, event):
print 'up'
def OnKeyDown(self, event):
print 'down'
class PicSampleApp(wx.App):
def __init__(self):
super(PicSampleApp, self).__init__(0)
def createFrame(self):
self.frame = WinFrame(None, "test")
self.frame.Show(True)
self.SetTopWindow(self.frame)
app = PicSampleApp()
app.createFrame()
app.MainLoop()
答案 0 :(得分:4)
我一直遇到捕获wx.EVT_KEY_DOWN和wx.EVT_KEY_UP的问题。解释为什么有点过头,但我知道切换到EVT_CHAR_HOOK通常对我有用。尝试这样的事情:
#!/usr/bin/env python
import sys, os
import wx
class WinFrame(wx.Frame):
def __init__(self, parent, title):
super(WinFrame, self).__init__(parent,
title=title,
size=(400,400))
self.panel = wx.Panel(self,-1, size=(400,400))
self.panel.Bind(wx.EVT_CHAR_HOOK, self.OnKeyPress)
def OnKeyPress(self, event):
keyCode = event.GetKeyCode()
if keyCode == wx.WXK_DOWN:
print 'down'
elif keyCode == wx.WXK_UP:
print 'up'
class PicSampleApp(wx.App):
def __init__(self):
super(PicSampleApp, self).__init__(0)
def createFrame(self):
self.frame = WinFrame(None, "test")
self.frame.Show(True)
self.SetTopWindow(self.frame)
app = PicSampleApp()
app.createFrame()
app.MainLoop()
如果您希望事件继续传播,您可能还希望将event.Skip()放在OnKeyPress的底部。