wxPython wx.KeyEvent GetKeyCode() [英] wxPython wx.KeyEvent GetKeyCode()

查看:298
本文介绍了wxPython wx.KeyEvent GetKeyCode()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个wxPython应用程序,该应用程序现在(其中)要在其中打印所按下键的名称.我有一本字典,例如将WXK_BACK映射到"back",这似乎很理智.但是,我必须导入(包括?)哪个文件才能获得WXK_BACK的定义?

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 dictionary 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 ?

我有import wx语句,但是不确定哪个特定文件包含机密

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

推荐答案

在导入wx模块(例如

>>> 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

然后在OnChar中,您可以执行此操作

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"

这篇关于wxPython wx.KeyEvent GetKeyCode()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆