Is there an easy way to capture all frame / window keystrokes in Python or wxPython

I tried EVT_KEY_DOWN but it does not work. Is there any way to capture any keystroke, like F1, F2, ENTER and others. I use Frame and Panel.

+6
source share
3 answers

I used EVT_KEY_DOWN in a subclass of the dialog. In the __init__ method of your dialog class, bind to EVT_KEY_DOWN:

 def __init__(self, ....): # ...other init code... self.Bind(wx.wx.EVT_KEY_UP, self.handle_key_up) 

Then specify a method in your dialog, for example:

 def handle_key_up(self, event): keycode = event.GetKeyCode() lc = self.list_ctrl_fields # handle F2 if keycode == wx.WXK_F2: print "got F2" 

(Tested in python 2.6, wxPython 2.8.10.)

+3
source

Is that what you mean? You need to look at global accelerators. Coincidentally, I looked at this on the last day or two. Assuming the wxpython application window has focus, the following should invoke the appropriate keystroke procedure. Work on my ubuntu 11.04 / py 2.7.1 / wxpython 2.8

Obviously, you can potentially consolidate the event method.

There is not much in this thread, but this link and this link helped me (same site)

 import wx class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, wx.ID_ANY, "Global Keypress") self.panel = wx.Panel(self, wx.ID_ANY) self.CreateStatusBar() # Global accelerators id_F1 = wx.NewId() id_F2 = wx.NewId() self.Bind(wx.EVT_MENU, self.pressed_F1, id=id_F1) self.Bind(wx.EVT_MENU, self.pressed_F2, id=id_F2) accel_tbl = wx.AcceleratorTable([ (wx.ACCEL_NORMAL, wx.WXK_F1, id_F1 ), (wx.ACCEL_NORMAL, wx.WXK_F2, id_F2 ) ]) self.SetAcceleratorTable(accel_tbl) def pressed_F1(self, event): print "Pressed F1" return True def pressed_F2(self, event): print "Pressed F2" return True if __name__ == "__main__": app = wx.PySimpleApp() f = MyFrame().Show() app.MainLoop() 
+3
source

Source: https://habr.com/ru/post/890978/


All Articles