www.emsdn.com
Class Profile: Home »» Python [Python] under "Python" »»» capture keyboard events
capture keyboard events
Hello,
I am trying to capture keyboard events. Here is some stripped down code
at my attempt.
It doesn't seem to react to key presses?
Any help would be very appreciated!
thnx,
Ray Sells
import wx
class Frame( wx.Frame):
def __init__( self, image = None, parent = None, id = -1,
pos = wx.DefaultPosition,
title = 'C++ Model Developer'):
"""Create a Frame instance and display an image."""
wx.Frameinit__( self, parent, id, title, pos, size = ( 400, 300))
panel = wx.Panel( self)
panel.button = wx.Button( panel, -1, "Close", pos = ( 100, 100))
self.Bind( wx.EVT_BUTTN, ButtonClick)
self.Bind( wx.EVT_KEY_DWN, Key)
def ButtonClick( self, event):
print "Button clicked!"
def Key( self, event):
k = event.GetKeyCode()
print "key code is", k
class App( wx.App):
def Init( self):
self.frame = Frame()
self.frame.Show()
self.SetTopWindow( self.frame)
return True
def main():
app = App( False)
app.MainLoop()
if __name__ == '__main__':
main()
No. 1# | By Developer Tags User at [2008-5-4] | size: 555 bytes
Hello Ray:
I think your problem is that you are binding to the frame, rather than
the button. This works for command events, such as button click, but
not for normal events, such as key down. The solution is to use
panel.button.Bind(wx.EVT_KEY_DWN, Key)
instead of
self.Bind(wx.EVT_KEY_DWN, Key)
Not tested, but it should work.
To unsubscribe, e-mail: wxPython-users-unsubscribe (AT) lists (DOT) wxwidgets.org
For additional commands, e-mail: wxPython-users-help (AT) lists (DOT) wxwidgets.org
Python Hot!
Python New!
Copyright © 2008 www.emsdn.com • All rights reserved • CMS Theme by www.emsdn.com - 0.422