开发者

wxPython wx.KeyEvent GetKeyCode()

开发者 https://www.devze.com 2022-12-11 07:19 出处:网络
I am writing a wxPython app in which I want (at the moment) to print the name of the key that was pressed.I have a di开发者_开发百科ctionary that maps, for example, the WXK_BACK to \"back\" which seem

I am writing a wxPython app in which I want (at the moment) to print the name of the key that was pressed. I have a di开发者_开发百科ctionary that maps, for example, the WXK_BACK to "back" which seems a sane. However, which file must I import (include?) to get the definition of WXK_BACK ?

I have the import wx statement, but am unsure which specific file holds the secrets


All key names can be directly used after importing wx module e.g

>>> import wx
>>> wx.WXK_BACK 
8

also you do not need to generate key to name map by hand, you generate keycode to name mapping automatically e.g.

import wx

keyMap = {}
for varName in vars(wx):
    if varName.startswith("WXK_"):
        keyMap[varName] = getattr(wx, varName)

print keyMap

Then in OnChar you can just do this

def OnChar(self, evt):
    try:
        print keyMap[evt.GetKeyCode()]
    except KeyError:
        print "keycode",evt.GetKeyCode(), "not found"


You only need to import wx for the WXK_BACK symbol. Code that looks something like the following should work.

import wx

class MyClass( wx.Window ):
    def __init__(self):
        self.Bind(wx.EVT_CHAR, self.OnChar)
    def OnChar(self, evt):
        x = evt.GetKeyCode()
        if x==wx.WXK_BACK:
            print "back"
0

精彩评论

暂无评论...
验证码 换一张
取 消