]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxKeyEvents.py
Tool tweaks and metadata update
[wxWidgets.git] / wxPython / demo / wxKeyEvents.py
1 # 11/19/2003 - Jeff Grimmett (grimmtooth@softhome.net)
2 #
3 # o Updated for wx namespace
4 #
5 # 11/29/2003 - Jeff Grimmett (grimmtooth@softhome.net)
6 #
7 # o lib.mixins.listctrl needs wx renamer applied.
8 #
9 # 12/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
10 #
11 # o wxListCtrlAutoWidthMixin -> ListCtrlAutoWidthMixin
12 #
13
14 import wx
15 import wx.lib.mixins.listctrl as listmix
16
17 #----------------------------------------------------------------------
18
19 keyMap = {
20 wx.WXK_BACK : "wx.WXK_BACK",
21 wx.WXK_TAB : "wx.WXK_TAB",
22 wx.WXK_RETURN : "wx.WXK_RETURN",
23 wx.WXK_ESCAPE : "wx.WXK_ESCAPE",
24 wx.WXK_SPACE : "wx.WXK_SPACE",
25 wx.WXK_DELETE : "wx.WXK_DELETE",
26 wx.WXK_START : "wx.WXK_START",
27 wx.WXK_LBUTTON : "wx.WXK_LBUTTON",
28 wx.WXK_RBUTTON : "wx.WXK_RBUTTON",
29 wx.WXK_CANCEL : "wx.WXK_CANCEL",
30 wx.WXK_MBUTTON : "wx.WXK_MBUTTON",
31 wx.WXK_CLEAR : "wx.WXK_CLEAR",
32 wx.WXK_SHIFT : "wx.WXK_SHIFT",
33 wx.WXK_ALT : "wx.WXK_ALT",
34 wx.WXK_CONTROL : "wx.WXK_CONTROL",
35 wx.WXK_MENU : "wx.WXK_MENU",
36 wx.WXK_PAUSE : "wx.WXK_PAUSE",
37 wx.WXK_CAPITAL : "wx.WXK_CAPITAL",
38 wx.WXK_PRIOR : "wx.WXK_PRIOR",
39 wx.WXK_NEXT : "wx.WXK_NEXT",
40 wx.WXK_END : "wx.WXK_END",
41 wx.WXK_HOME : "wx.WXK_HOME",
42 wx.WXK_LEFT : "wx.WXK_LEFT",
43 wx.WXK_UP : "wx.WXK_UP",
44 wx.WXK_RIGHT : "wx.WXK_RIGHT",
45 wx.WXK_DOWN : "wx.WXK_DOWN",
46 wx.WXK_SELECT : "wx.WXK_SELECT",
47 wx.WXK_PRINT : "wx.WXK_PRINT",
48 wx.WXK_EXECUTE : "wx.WXK_EXECUTE",
49 wx.WXK_SNAPSHOT : "wx.WXK_SNAPSHOT",
50 wx.WXK_INSERT : "wx.WXK_INSERT",
51 wx.WXK_HELP : "wx.WXK_HELP",
52 wx.WXK_NUMPAD0 : "wx.WXK_NUMPAD0",
53 wx.WXK_NUMPAD1 : "wx.WXK_NUMPAD1",
54 wx.WXK_NUMPAD2 : "wx.WXK_NUMPAD2",
55 wx.WXK_NUMPAD3 : "wx.WXK_NUMPAD3",
56 wx.WXK_NUMPAD4 : "wx.WXK_NUMPAD4",
57 wx.WXK_NUMPAD5 : "wx.WXK_NUMPAD5",
58 wx.WXK_NUMPAD6 : "wx.WXK_NUMPAD6",
59 wx.WXK_NUMPAD7 : "wx.WXK_NUMPAD7",
60 wx.WXK_NUMPAD8 : "wx.WXK_NUMPAD8",
61 wx.WXK_NUMPAD9 : "wx.WXK_NUMPAD9",
62 wx.WXK_MULTIPLY : "wx.WXK_MULTIPLY",
63 wx.WXK_ADD : "wx.WXK_ADD",
64 wx.WXK_SEPARATOR : "wx.WXK_SEPARATOR",
65 wx.WXK_SUBTRACT : "wx.WXK_SUBTRACT",
66 wx.WXK_DECIMAL : "wx.WXK_DECIMAL",
67 wx.WXK_DIVIDE : "wx.WXK_DIVIDE",
68 wx.WXK_F1 : "wx.WXK_F1",
69 wx.WXK_F2 : "wx.WXK_F2",
70 wx.WXK_F3 : "wx.WXK_F3",
71 wx.WXK_F4 : "wx.WXK_F4",
72 wx.WXK_F5 : "wx.WXK_F5",
73 wx.WXK_F6 : "wx.WXK_F6",
74 wx.WXK_F7 : "wx.WXK_F7",
75 wx.WXK_F8 : "wx.WXK_F8",
76 wx.WXK_F9 : "wx.WXK_F9",
77 wx.WXK_F10 : "wx.WXK_F10",
78 wx.WXK_F11 : "wx.WXK_F11",
79 wx.WXK_F12 : "wx.WXK_F12",
80 wx.WXK_F13 : "wx.WXK_F13",
81 wx.WXK_F14 : "wx.WXK_F14",
82 wx.WXK_F15 : "wx.WXK_F15",
83 wx.WXK_F16 : "wx.WXK_F16",
84 wx.WXK_F17 : "wx.WXK_F17",
85 wx.WXK_F18 : "wx.WXK_F18",
86 wx.WXK_F19 : "wx.WXK_F19",
87 wx.WXK_F20 : "wx.WXK_F20",
88 wx.WXK_F21 : "wx.WXK_F21",
89 wx.WXK_F22 : "wx.WXK_F22",
90 wx.WXK_F23 : "wx.WXK_F23",
91 wx.WXK_F24 : "wx.WXK_F24",
92 wx.WXK_NUMLOCK : "wx.WXK_NUMLOCK",
93 wx.WXK_SCROLL : "wx.WXK_SCROLL",
94 wx.WXK_PAGEUP : "wx.WXK_PAGEUP",
95 wx.WXK_PAGEDOWN : "wx.WXK_PAGEDOWN",
96 wx.WXK_NUMPAD_SPACE : "wx.WXK_NUMPAD_SPACE",
97 wx.WXK_NUMPAD_TAB : "wx.WXK_NUMPAD_TAB",
98 wx.WXK_NUMPAD_ENTER : "wx.WXK_NUMPAD_ENTER",
99 wx.WXK_NUMPAD_F1 : "wx.WXK_NUMPAD_F1",
100 wx.WXK_NUMPAD_F2 : "wx.WXK_NUMPAD_F2",
101 wx.WXK_NUMPAD_F3 : "wx.WXK_NUMPAD_F3",
102 wx.WXK_NUMPAD_F4 : "wx.WXK_NUMPAD_F4",
103 wx.WXK_NUMPAD_HOME : "wx.WXK_NUMPAD_HOME",
104 wx.WXK_NUMPAD_LEFT : "wx.WXK_NUMPAD_LEFT",
105 wx.WXK_NUMPAD_UP : "wx.WXK_NUMPAD_UP",
106 wx.WXK_NUMPAD_RIGHT : "wx.WXK_NUMPAD_RIGHT",
107 wx.WXK_NUMPAD_DOWN : "wx.WXK_NUMPAD_DOWN",
108 wx.WXK_NUMPAD_PRIOR : "wx.WXK_NUMPAD_PRIOR",
109 wx.WXK_NUMPAD_PAGEUP : "wx.WXK_NUMPAD_PAGEUP",
110 wx.WXK_NUMPAD_NEXT : "wx.WXK_NUMPAD_NEXT",
111 wx.WXK_NUMPAD_PAGEDOWN : "wx.WXK_NUMPAD_PAGEDOWN",
112 wx.WXK_NUMPAD_END : "wx.WXK_NUMPAD_END",
113 wx.WXK_NUMPAD_BEGIN : "wx.WXK_NUMPAD_BEGIN",
114 wx.WXK_NUMPAD_INSERT : "wx.WXK_NUMPAD_INSERT",
115 wx.WXK_NUMPAD_DELETE : "wx.WXK_NUMPAD_DELETE",
116 wx.WXK_NUMPAD_EQUAL : "wx.WXK_NUMPAD_EQUAL",
117 wx.WXK_NUMPAD_MULTIPLY : "wx.WXK_NUMPAD_MULTIPLY",
118 wx.WXK_NUMPAD_ADD : "wx.WXK_NUMPAD_ADD",
119 wx.WXK_NUMPAD_SEPARATOR : "wx.WXK_NUMPAD_SEPARATOR",
120 wx.WXK_NUMPAD_SUBTRACT : "wx.WXK_NUMPAD_SUBTRACT",
121 wx.WXK_NUMPAD_DECIMAL : "wx.WXK_NUMPAD_DECIMAL",
122 wx.WXK_NUMPAD_DIVIDE : "wx.WXK_NUMPAD_DIVIDE",
123 }
124
125
126 #----------------------------------------------------------------------
127
128 class KeySink(wx.Window):
129 def __init__(self, parent):
130 wx.Window.__init__(self, parent, -1, style=wx.WANTS_CHARS
131 #| wx.RAISED_BORDER
132 #| wx.SUNKEN_BORDER
133 )
134
135 self.SetBackgroundColour(wx.BLUE)
136 self.haveFocus = False
137 self.callSkip = False
138 self.logKeyDn = True
139 self.logKeyUp = True
140 self.logChar = True
141
142 self.Bind(wx.EVT_PAINT, self.OnPaint)
143 self.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
144 self.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
145 self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouse)
146
147 self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
148 self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
149 self.Bind(wx.EVT_CHAR, self.OnChar)
150
151
152 def SetCallSkip(self, skip):
153 self.callSkip = skip
154
155 def SetLogKeyUp(self, val):
156 self.logKeyUp = val
157
158 def SetLogKeyDn(self, val):
159 self.logKeyDn = val
160
161 def SetLogChar(self, val):
162 self.logChar = val
163
164
165 def OnPaint(self, evt):
166 dc = wx.PaintDC(self)
167 rect = self.GetClientRect()
168 dc.SetTextForeground(wx.WHITE)
169 dc.DrawLabel("Click here and then press some keys",
170 rect, wx.ALIGN_CENTER | wx.ALIGN_TOP)
171 if self.haveFocus:
172 dc.SetTextForeground(wx.GREEN)
173 dc.DrawLabel("Have Focus", rect, wx.ALIGN_RIGHT | wx.ALIGN_BOTTOM)
174 else:
175 dc.SetTextForeground(wx.RED)
176 dc.DrawLabel("Need Focus!", rect, wx.ALIGN_RIGHT | wx.ALIGN_BOTTOM)
177
178
179 def OnSetFocus(self, evt):
180 self.haveFocus = True
181 self.Refresh()
182
183 def OnKillFocus(self, evt):
184 self.haveFocus = False
185 self.Refresh()
186
187 def OnMouse(self, evt):
188 if evt.ButtonDown():
189 self.SetFocus()
190
191
192 def OnKeyDown(self, evt):
193 if self.logKeyDn:
194 self.GetParent().keylog.LogKeyEvent("KeyDown", evt)
195 if self.callSkip:
196 evt.Skip()
197
198 def OnKeyUp(self, evt):
199 if self.logKeyUp:
200 self.GetParent().keylog.LogKeyEvent("KeyUp", evt)
201 if self.callSkip:
202 evt.Skip()
203
204 def OnChar(self, evt):
205 if self.logChar:
206 self.GetParent().keylog.LogKeyEvent("Char", evt)
207
208
209 #----------------------------------------------------------------------
210
211 class KeyLog(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
212
213 def __init__(self, parent):
214 wx.ListCtrl.__init__(self, parent, -1,
215 style = wx.LC_REPORT|wx.LC_VRULES|wx.LC_HRULES)
216 listmix.ListCtrlAutoWidthMixin.__init__(self)
217
218 self.InsertColumn(0, "Event Type")
219 self.InsertColumn(1, "Key Name")
220 self.InsertColumn(2, "Key Code")
221 self.InsertColumn(3, "Modifiers")
222 self.InsertColumn(4, "RawKeyCode")
223 self.InsertColumn(5, "RawKeyFlags")
224 self.InsertColumn(6, "")
225
226 for x in range(6):
227 self.SetColumnWidth(x, wx.LIST_AUTOSIZE_USEHEADER)
228
229 self.SetColumnWidth(1, 125)
230
231
232 def LogKeyEvent(self, evType, evt):
233 keycode = evt.GetKeyCode()
234 keyname = keyMap.get(keycode, None)
235 if keyname is None:
236 if keycode < 256:
237 if keycode == 0:
238 keyname = "NUL"
239 elif keycode < 27:
240 keyname = "Ctrl-%s" % chr(ord('A') + keycode-1)
241 else:
242 keyname = "\"%s\"" % chr(keycode)
243 else:
244 keyname = "unknown (%s)" % keycode
245
246 modifiers = ""
247 for mod, ch in [(evt.ControlDown(), 'C'),
248 (evt.AltDown(), 'A'),
249 (evt.ShiftDown(), 'S'),
250 (evt.MetaDown(), 'M')]:
251 if mod:
252 modifiers += ch
253 else:
254 modifiers += '-'
255
256 id = self.InsertStringItem(self.GetItemCount(), evType)
257 self.SetStringItem(id, 1, keyname)
258 self.SetStringItem(id, 2, str(keycode))
259 self.SetStringItem(id, 3, modifiers)
260 self.SetStringItem(id, 4, str(evt.GetRawKeyCode()))
261 self.SetStringItem(id, 5, str(evt.GetRawKeyFlags()))
262
263 #print ( id, evType, keyname, str(keycode), modifiers, str(evt.GetRawKeyCode()), str(evt.GetRawKeyFlags()))
264
265 self.EnsureVisible(id)
266
267
268 def ClearLog(self):
269 self.DeleteAllItems()
270
271
272 #----------------------------------------------------------------------
273
274
275 class TestPanel(wx.Panel):
276 def __init__(self, parent, log):
277 self.log = log
278 wx.Panel.__init__(self, parent, -1, style=0)
279 self.keysink = KeySink(self)
280 self.keysink.SetSize((100, 65))
281 self.keylog = KeyLog(self)
282
283 btn = wx.Button(self, -1, "Clear Log")
284 self.Bind(wx.EVT_BUTTON, self.OnClearBtn, btn)
285
286 cb1 = wx.CheckBox(self, -1, "Call evt.Skip for Key Up/Dn events")
287 self.Bind(wx.EVT_CHECKBOX, self.OnSkipCB, cb1)
288
289 cb2 = wx.CheckBox(self, -1, "EVT_KEY_UP")
290 self.Bind(wx.EVT_CHECKBOX, self.OnKeyUpCB, cb2)
291 cb2.SetValue(True)
292
293 cb3 = wx.CheckBox(self, -1, "EVT_KEY_DOWN")
294 self.Bind(wx.EVT_CHECKBOX, self.OnKeyDnCB, cb3)
295 cb3.SetValue(True)
296
297 cb4 = wx.CheckBox(self, -1, "EVT_CHAR")
298 self.Bind(wx.EVT_CHECKBOX, self.OnCharCB, cb4)
299 cb4.SetValue(True)
300
301 buttons = wx.BoxSizer(wx.HORIZONTAL)
302 buttons.Add(btn, 0, wx.ALL, 4)
303 buttons.Add(cb1, 0, wx.ALIGN_CENTER_VERTICAL|wx.LEFT|wx.RIGHT, 6)
304 buttons.Add(cb2, 0, wx.ALIGN_CENTER_VERTICAL|wx.LEFT, 6)
305 buttons.Add(cb3, 0, wx.ALIGN_CENTER_VERTICAL|wx.LEFT, 6)
306 buttons.Add(cb4, 0, wx.ALIGN_CENTER_VERTICAL|wx.LEFT, 6)
307
308 sizer = wx.BoxSizer(wx.VERTICAL)
309 sizer.Add(self.keysink, 0, wx.GROW)
310 sizer.Add(buttons)
311 sizer.Add(self.keylog, 1, wx.GROW)
312
313 self.SetSizer(sizer)
314
315
316 def OnClearBtn(self, evt):
317 self.keylog.ClearLog()
318
319 def OnSkipCB(self, evt):
320 self.keysink.SetCallSkip(evt.GetInt())
321
322 def OnKeyUpCB(self, evt):
323 self.keysink.SetLogKeyUp(evt.GetInt())
324
325 def OnKeyDnCB(self, evt):
326 self.keysink.SetLogKeyDn(evt.GetInt())
327
328 def OnCharCB(self, evt):
329 self.keysink.SetLogChar(evt.GetInt())
330
331
332 #----------------------------------------------------------------------
333
334 def runTest(frame, nb, log):
335 win = TestPanel(nb, log)
336 return win
337
338 #----------------------------------------------------------------------
339
340
341
342 overview = """<html><body>
343 <h2><center>wxKeyEvents</center></h2>
344
345 This demo simply catches all key events and prints info about them.
346 It is meant to be used as a compatibility test for cross platform work.
347
348 </body></html>
349 """
350
351
352
353 if __name__ == '__main__':
354 import sys,os
355 import run
356 run.main(['', os.path.basename(sys.argv[0])])
357