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
=
122 self
.SetBackgroundColour(wxBLUE
)
123 self
.haveFocus
= false
124 self
.callSkip
= false
129 EVT_PAINT(self
, self
.OnPaint
)
130 EVT_SET_FOCUS(self
, self
.OnSetFocus
)
131 EVT_KILL_FOCUS(self
, self
.OnKillFocus
)
132 EVT_MOUSE_EVENTS(self
, self
.OnMouse
)
134 EVT_KEY_DOWN(self
, self
.OnKeyDown
)
135 EVT_KEY_UP(self
, self
.OnKeyUp
)
136 EVT_CHAR(self
, self
.OnChar
)
139 def SetCallSkip(self
, skip
):
142 def SetLogKeyUp(self
, val
):
145 def SetLogKeyDn(self
, val
):
148 def SetLogChar(self
, val
):
152 def OnPaint(self
, evt
):
154 rect
= self
.GetClientRect()
155 dc
.SetTextForeground(wxWHITE
)
156 dc
.DrawLabel("Click here and then press some keys",
157 rect
, wxALIGN_CENTER | wxALIGN_TOP
)
159 dc
.SetTextForeground(wxGREEN
)
160 dc
.DrawLabel("Have Focus", rect
, wxALIGN_RIGHT | wxALIGN_BOTTOM
)
162 dc
.SetTextForeground(wxRED
)
163 dc
.DrawLabel("Need Focus!", rect
, wxALIGN_RIGHT | wxALIGN_BOTTOM
)
166 def OnSetFocus(self
, evt
):
167 self
.haveFocus
= true
170 def OnKillFocus(self
, evt
):
171 self
.haveFocus
= false
174 def OnMouse(self
, evt
):
179 def OnKeyDown(self
, evt
):
181 self
.GetParent().keylog
.LogKeyEvent("KeyDown", evt
)
185 def OnKeyUp(self
, evt
):
187 self
.GetParent().keylog
.LogKeyEvent("KeyUp", evt
)
191 def OnChar(self
, evt
):
193 self
.GetParent().keylog
.LogKeyEvent("Char", evt
)
196 #----------------------------------------------------------------------
198 from wxPython
.lib
.mixins
.listctrl
import wxListCtrlAutoWidthMixin
200 class KeyLog(wxListCtrl
, wxListCtrlAutoWidthMixin
):
202 def __init__(self
, parent
):
203 wxListCtrl
.__init
__(self
, parent
, -1,
204 style
= wxLC_REPORT|wxLC_VRULES|wxLC_HRULES
)
205 wxListCtrlAutoWidthMixin
.__init
__(self
)
207 self
.InsertColumn(0, "Event Type")
208 self
.InsertColumn(1, "Key Name")
209 self
.InsertColumn(2, "Key Code")
210 self
.InsertColumn(3, "Modifiers")
211 self
.InsertColumn(4, "RawKeyCode")
212 self
.InsertColumn(5, "RawKeyFlags")
213 self
.InsertColumn(6, "")
216 self
.SetColumnWidth(x
, wxLIST_AUTOSIZE_USEHEADER
)
218 self
.SetColumnWidth(1, 125)
221 def LogKeyEvent(self
, evType
, evt
):
222 keycode
= evt
.GetKeyCode()
223 keyname
= keyMap
.get(keycode
, None)
229 keyname
= "Ctrl-%s" % chr(ord('A') + keycode
-1)
231 keyname
= "\"%s\"" % chr(keycode
)
233 keyname
= "unknown (%s)" % keycode
236 for mod
, ch
in [(evt
.ControlDown(), 'C'),
237 (evt
.AltDown(), 'A'),
238 (evt
.ShiftDown(), 'S'),
239 (evt
.MetaDown(), 'M')]:
245 id = self
.InsertStringItem(self
.GetItemCount(), evType
)
246 self
.SetStringItem(id, 1, keyname
)
247 self
.SetStringItem(id, 2, str(keycode
))
248 self
.SetStringItem(id, 3, modifiers
)
249 self
.SetStringItem(id, 4, str(evt
.GetRawKeyCode()))
250 self
.SetStringItem(id, 5, str(evt
.GetRawKeyFlags()))
252 #print ( id, evType, keyname, str(keycode), modifiers, str(evt.GetRawKeyCode()), str(evt.GetRawKeyFlags()))
254 self
.EnsureVisible(id)
258 self
.DeleteAllItems()
261 #----------------------------------------------------------------------
264 class TestPanel(wxPanel
):
265 def __init__(self
, parent
, log
):
267 wxPanel
.__init
__(self
, parent
, -1, style
=0)
268 self
.keysink
= KeySink(self
)
269 self
.keysink
.SetSize((100, 65))
270 self
.keylog
= KeyLog(self
)
272 btn
= wxButton(self
, -1, "Clear Log")
273 EVT_BUTTON(self
, btn
.GetId(), self
.OnClearBtn
)
275 cb1
= wxCheckBox(self
, -1, "Call evt.Skip for Key Up/Dn events")
276 EVT_CHECKBOX(self
, cb1
.GetId(), self
.OnSkipCB
)
278 cb2
= wxCheckBox(self
, -1, "EVT_KEY_UP")
279 EVT_CHECKBOX(self
, cb2
.GetId(), self
.OnKeyUpCB
)
282 cb3
= wxCheckBox(self
, -1, "EVT_KEY_DOWN")
283 EVT_CHECKBOX(self
, cb3
.GetId(), self
.OnKeyDnCB
)
286 cb4
= wxCheckBox(self
, -1, "EVT_CHAR")
287 EVT_CHECKBOX(self
, cb4
.GetId(), self
.OnCharCB
)
290 buttons
= wxBoxSizer(wxHORIZONTAL
)
291 buttons
.Add(btn
, 0, wxALL
, 4)
292 buttons
.Add(cb1
, 0, wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT
, 6)
293 buttons
.Add(cb2
, 0, wxALIGN_CENTER_VERTICAL|wxLEFT
, 6)
294 buttons
.Add(cb3
, 0, wxALIGN_CENTER_VERTICAL|wxLEFT
, 6)
295 buttons
.Add(cb4
, 0, wxALIGN_CENTER_VERTICAL|wxLEFT
, 6)
297 sizer
= wxBoxSizer(wxVERTICAL
)
298 sizer
.Add(self
.keysink
, 0, wxGROW
)
300 sizer
.Add(self
.keylog
, 1, wxGROW
)
305 def OnClearBtn(self
, evt
):
306 self
.keylog
.ClearLog()
308 def OnSkipCB(self
, evt
):
309 self
.keysink
.SetCallSkip(evt
.GetInt())
311 def OnKeyUpCB(self
, evt
):
312 self
.keysink
.SetLogKeyUp(evt
.GetInt())
314 def OnKeyDnCB(self
, evt
):
315 self
.keysink
.SetLogKeyDn(evt
.GetInt())
317 def OnCharCB(self
, evt
):
318 self
.keysink
.SetLogChar(evt
.GetInt())
321 #----------------------------------------------------------------------
323 def runTest(frame
, nb
, log
):
324 win
= TestPanel(nb
, log
)
327 #----------------------------------------------------------------------
331 overview
= """<html><body>
332 <h2><center>wxKeyEvents</center></h2>
334 This demo simply lets catches all key events and prints info about them.
335 It is meant to be used as a compatibility test for cross platform work.
342 if __name__
== '__main__':
345 run
.main(['', os
.path
.basename(sys
.argv
[0])])