2 from wxPython
.wx
import *
4 #----------------------------------------------------------------------
9 WXK_RETURN
: "WXK_RETURN",
10 WXK_ESCAPE
: "WXK_ESCAPE",
11 WXK_SPACE
: "WXK_SPACE",
12 WXK_DELETE
: "WXK_DELETE",
13 WXK_START
: "WXK_START",
14 WXK_LBUTTON
: "WXK_LBUTTON",
15 WXK_RBUTTON
: "WXK_RBUTTON",
16 WXK_CANCEL
: "WXK_CANCEL",
17 WXK_MBUTTON
: "WXK_MBUTTON",
18 WXK_CLEAR
: "WXK_CLEAR",
19 WXK_SHIFT
: "WXK_SHIFT",
21 WXK_CONTROL
: "WXK_CONTROL",
22 WXK_MENU
: "WXK_MENU",
23 WXK_PAUSE
: "WXK_PAUSE",
24 WXK_CAPITAL
: "WXK_CAPITAL",
25 WXK_PRIOR
: "WXK_PRIOR",
26 WXK_NEXT
: "WXK_NEXT",
28 WXK_HOME
: "WXK_HOME",
29 WXK_LEFT
: "WXK_LEFT",
31 WXK_RIGHT
: "WXK_RIGHT",
32 WXK_DOWN
: "WXK_DOWN",
33 WXK_SELECT
: "WXK_SELECT",
34 WXK_PRINT
: "WXK_PRINT",
35 WXK_EXECUTE
: "WXK_EXECUTE",
36 WXK_SNAPSHOT
: "WXK_SNAPSHOT",
37 WXK_INSERT
: "WXK_INSERT",
38 WXK_HELP
: "WXK_HELP",
39 WXK_NUMPAD0
: "WXK_NUMPAD0",
40 WXK_NUMPAD1
: "WXK_NUMPAD1",
41 WXK_NUMPAD2
: "WXK_NUMPAD2",
42 WXK_NUMPAD3
: "WXK_NUMPAD3",
43 WXK_NUMPAD4
: "WXK_NUMPAD4",
44 WXK_NUMPAD5
: "WXK_NUMPAD5",
45 WXK_NUMPAD6
: "WXK_NUMPAD6",
46 WXK_NUMPAD7
: "WXK_NUMPAD7",
47 WXK_NUMPAD8
: "WXK_NUMPAD8",
48 WXK_NUMPAD9
: "WXK_NUMPAD9",
49 WXK_MULTIPLY
: "WXK_MULTIPLY",
51 WXK_SEPARATOR
: "WXK_SEPARATOR",
52 WXK_SUBTRACT
: "WXK_SUBTRACT",
53 WXK_DECIMAL
: "WXK_DECIMAL",
54 WXK_DIVIDE
: "WXK_DIVIDE",
79 WXK_NUMLOCK
: "WXK_NUMLOCK",
80 WXK_SCROLL
: "WXK_SCROLL",
81 WXK_PAGEUP
: "WXK_PAGEUP",
82 WXK_PAGEDOWN
: "WXK_PAGEDOWN",
83 WXK_NUMPAD_SPACE
: "WXK_NUMPAD_SPACE",
84 WXK_NUMPAD_TAB
: "WXK_NUMPAD_TAB",
85 WXK_NUMPAD_ENTER
: "WXK_NUMPAD_ENTER",
86 WXK_NUMPAD_F1
: "WXK_NUMPAD_F1",
87 WXK_NUMPAD_F2
: "WXK_NUMPAD_F2",
88 WXK_NUMPAD_F3
: "WXK_NUMPAD_F3",
89 WXK_NUMPAD_F4
: "WXK_NUMPAD_F4",
90 WXK_NUMPAD_HOME
: "WXK_NUMPAD_HOME",
91 WXK_NUMPAD_LEFT
: "WXK_NUMPAD_LEFT",
92 WXK_NUMPAD_UP
: "WXK_NUMPAD_UP",
93 WXK_NUMPAD_RIGHT
: "WXK_NUMPAD_RIGHT",
94 WXK_NUMPAD_DOWN
: "WXK_NUMPAD_DOWN",
95 WXK_NUMPAD_PRIOR
: "WXK_NUMPAD_PRIOR",
96 WXK_NUMPAD_PAGEUP
: "WXK_NUMPAD_PAGEUP",
97 WXK_NUMPAD_NEXT
: "WXK_NUMPAD_NEXT",
98 WXK_NUMPAD_PAGEDOWN
: "WXK_NUMPAD_PAGEDOWN",
99 WXK_NUMPAD_END
: "WXK_NUMPAD_END",
100 WXK_NUMPAD_BEGIN
: "WXK_NUMPAD_BEGIN",
101 WXK_NUMPAD_INSERT
: "WXK_NUMPAD_INSERT",
102 WXK_NUMPAD_DELETE
: "WXK_NUMPAD_DELETE",
103 WXK_NUMPAD_EQUAL
: "WXK_NUMPAD_EQUAL",
104 WXK_NUMPAD_MULTIPLY
: "WXK_NUMPAD_MULTIPLY",
105 WXK_NUMPAD_ADD
: "WXK_NUMPAD_ADD",
106 WXK_NUMPAD_SEPARATOR
: "WXK_NUMPAD_SEPARATOR",
107 WXK_NUMPAD_SUBTRACT
: "WXK_NUMPAD_SUBTRACT",
108 WXK_NUMPAD_DECIMAL
: "WXK_NUMPAD_DECIMAL",
109 WXK_NUMPAD_DIVIDE
: "WXK_NUMPAD_DIVIDE",
113 #----------------------------------------------------------------------
115 class KeySink(wxWindow
):
116 def __init__(self
, parent
):
117 wxWindow
.__init
__(self
, parent
, -1, style
=wxWANTS_CHARS
)
118 self
.SetBackgroundColour(wxBLUE
)
119 self
.haveFocus
= false
120 self
.callSkip
= false
125 EVT_PAINT(self
, self
.OnPaint
)
126 EVT_SET_FOCUS(self
, self
.OnSetFocus
)
127 EVT_KILL_FOCUS(self
, self
.OnKillFocus
)
128 EVT_MOUSE_EVENTS(self
, self
.OnMouse
)
130 EVT_KEY_DOWN(self
, self
.OnKeyDown
)
131 EVT_KEY_UP(self
, self
.OnKeyUp
)
132 EVT_CHAR(self
, self
.OnChar
)
135 def SetCallSkip(self
, skip
):
138 def SetLogKeyUp(self
, val
):
141 def SetLogKeyDn(self
, val
):
144 def SetLogChar(self
, val
):
148 def OnPaint(self
, evt
):
150 rect
= self
.GetClientRect()
151 dc
.SetTextForeground(wxWHITE
)
152 dc
.DrawLabel("Click here and then press some keys",
153 rect
, wxALIGN_CENTER | wxALIGN_TOP
)
155 dc
.SetTextForeground(wxGREEN
)
156 dc
.DrawLabel("Have Focus", rect
, wxALIGN_RIGHT | wxALIGN_BOTTOM
)
158 dc
.SetTextForeground(wxRED
)
159 dc
.DrawLabel("Need Focus!", rect
, wxALIGN_RIGHT | wxALIGN_BOTTOM
)
162 def OnSetFocus(self
, evt
):
163 self
.haveFocus
= true
166 def OnKillFocus(self
, evt
):
167 self
.haveFocus
= false
170 def OnMouse(self
, evt
):
175 def OnKeyDown(self
, evt
):
177 self
.GetParent().keylog
.LogKeyEvent("KeyDown", evt
)
181 def OnKeyUp(self
, evt
):
183 self
.GetParent().keylog
.LogKeyEvent("KeyUp", evt
)
187 def OnChar(self
, evt
):
189 self
.GetParent().keylog
.LogKeyEvent("Char", evt
)
192 #----------------------------------------------------------------------
194 from wxPython
.lib
.mixins
.listctrl
import wxListCtrlAutoWidthMixin
196 class KeyLog(wxListCtrl
, wxListCtrlAutoWidthMixin
):
198 def __init__(self
, parent
):
199 wxListCtrl
.__init
__(self
, parent
, -1,
200 style
= wxLC_REPORT|wxLC_VRULES|wxLC_HRULES
)
201 wxListCtrlAutoWidthMixin
.__init
__(self
)
203 self
.InsertColumn(0, "Event Type")
204 self
.InsertColumn(1, "Key Name")
205 self
.InsertColumn(2, "Key Code")
206 self
.InsertColumn(3, "Modifiers")
207 self
.InsertColumn(4, "RawKeyCode")
208 self
.InsertColumn(5, "RawKeyFlags")
209 self
.InsertColumn(6, "")
212 self
.SetColumnWidth(x
, wxLIST_AUTOSIZE_USEHEADER
)
214 self
.SetColumnWidth(1, 125)
217 def LogKeyEvent(self
, evType
, evt
):
218 keycode
= evt
.GetKeyCode()
219 keyname
= keyMap
.get(keycode
, None)
225 keyname
= "Ctrl-%s" % chr(ord('A') + keycode
-1)
227 keyname
= "\"%s\"" % chr(keycode
)
229 keyname
= "unknown (%s)" % keycode
232 for mod
, ch
in [(evt
.ControlDown(), 'C'),
233 (evt
.AltDown(), 'A'),
234 (evt
.ShiftDown(), 'S'),
235 (evt
.MetaDown(), 'M')]:
241 id = self
.InsertStringItem(self
.GetItemCount(), evType
)
242 self
.SetStringItem(id, 1, keyname
)
243 self
.SetStringItem(id, 2, str(keycode
))
244 self
.SetStringItem(id, 3, modifiers
)
245 self
.SetStringItem(id, 4, str(evt
.GetRawKeyCode()))
246 self
.SetStringItem(id, 5, str(evt
.GetRawKeyFlags()))
248 #print ( id, evType, keyname, str(keycode), modifiers, str(evt.GetRawKeyCode()), str(evt.GetRawKeyFlags()))
250 self
.EnsureVisible(id)
254 self
.DeleteAllItems()
257 #----------------------------------------------------------------------
260 class TestPanel(wxPanel
):
261 def __init__(self
, parent
, log
):
263 wxPanel
.__init
__(self
, parent
, -1, style
=0)
264 self
.keysink
= KeySink(self
)
265 self
.keysink
.SetSize((100, 65))
266 self
.keylog
= KeyLog(self
)
268 btn
= wxButton(self
, -1, "Clear Log")
269 EVT_BUTTON(self
, btn
.GetId(), self
.OnClearBtn
)
271 cb1
= wxCheckBox(self
, -1, "Call evt.Skip for Key Up/Dn events")
272 EVT_CHECKBOX(self
, cb1
.GetId(), self
.OnSkipCB
)
274 cb2
= wxCheckBox(self
, -1, "EVT_KEY_UP")
275 EVT_CHECKBOX(self
, cb2
.GetId(), self
.OnKeyUpCB
)
278 cb3
= wxCheckBox(self
, -1, "EVT_KEY_DOWN")
279 EVT_CHECKBOX(self
, cb3
.GetId(), self
.OnKeyDnCB
)
282 cb4
= wxCheckBox(self
, -1, "EVT_CHAR")
283 EVT_CHECKBOX(self
, cb4
.GetId(), self
.OnCharCB
)
286 buttons
= wxBoxSizer(wxHORIZONTAL
)
287 buttons
.Add(btn
, 0, wxALL
, 4)
288 buttons
.Add(cb1
, 0, wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT
, 6)
289 buttons
.Add(cb2
, 0, wxALIGN_CENTER_VERTICAL|wxLEFT
, 6)
290 buttons
.Add(cb3
, 0, wxALIGN_CENTER_VERTICAL|wxLEFT
, 6)
291 buttons
.Add(cb4
, 0, wxALIGN_CENTER_VERTICAL|wxLEFT
, 6)
293 sizer
= wxBoxSizer(wxVERTICAL
)
294 sizer
.Add(self
.keysink
, 0, wxGROW
)
296 sizer
.Add(self
.keylog
, 1, wxGROW
)
301 def OnClearBtn(self
, evt
):
302 self
.keylog
.ClearLog()
304 def OnSkipCB(self
, evt
):
305 self
.keysink
.SetCallSkip(evt
.GetInt())
307 def OnKeyUpCB(self
, evt
):
308 self
.keysink
.SetLogKeyUp(evt
.GetInt())
310 def OnKeyDnCB(self
, evt
):
311 self
.keysink
.SetLogKeyDn(evt
.GetInt())
313 def OnCharCB(self
, evt
):
314 self
.keysink
.SetLogChar(evt
.GetInt())
317 #----------------------------------------------------------------------
319 def runTest(frame
, nb
, log
):
320 win
= TestPanel(nb
, log
)
323 #----------------------------------------------------------------------
327 overview
= """<html><body>
328 <h2><center>wxKeyEvents</center></h2>
330 This demo simply lets catches all key events and prints info about them.
331 It is meant to be used as a compatibility test for cross platform work.
338 if __name__
== '__main__':
341 run
.main(['', os
.path
.basename(sys
.argv
[0])])