0

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 ?

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

2 Answers 2

3

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That is easier, AND I learned something. I appreciate your all your help. :bp:
0

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"

1 Comment

Thanks! I am still a bit new to this python/wxpython, and the wx. prefix eluded me. (I was using WXK_BACK rather than wx.WXK_BACK). Thank you very much for your help. :bp:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.