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

CharlesB
  • 86,532
  • 28
  • 194
  • 218
Billy Pilgrim
  • 1,842
  • 3
  • 22
  • 32

2 Answers2

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"
Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
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"
tom10
  • 67,082
  • 10
  • 127
  • 137
  • 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: – Billy Pilgrim Nov 11 '09 at 16:55