]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxKeyEvents.py
Compile fix
[wxWidgets.git] / wxPython / demo / wxKeyEvents.py
1
2 from wxPython.wx import *
3
4 #----------------------------------------------------------------------
5
6 keyMap = {
7 WXK_BACK : "WXK_BACK",
8 WXK_TAB : "WXK_TAB",
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",
20 WXK_ALT : "WXK_ALT",
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",
27 WXK_END : "WXK_END",
28 WXK_HOME : "WXK_HOME",
29 WXK_LEFT : "WXK_LEFT",
30 WXK_UP : "WXK_UP",
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",
50 WXK_ADD : "WXK_ADD",
51 WXK_SEPARATOR : "WXK_SEPARATOR",
52 WXK_SUBTRACT : "WXK_SUBTRACT",
53 WXK_DECIMAL : "WXK_DECIMAL",
54 WXK_DIVIDE : "WXK_DIVIDE",
55 WXK_F1 : "WXK_F1",
56 WXK_F2 : "WXK_F2",
57 WXK_F3 : "WXK_F3",
58 WXK_F4 : "WXK_F4",
59 WXK_F5 : "WXK_F5",
60 WXK_F6 : "WXK_F6",
61 WXK_F7 : "WXK_F7",
62 WXK_F8 : "WXK_F8",
63 WXK_F9 : "WXK_F9",
64 WXK_F10 : "WXK_F10",
65 WXK_F11 : "WXK_F11",
66 WXK_F12 : "WXK_F12",
67 WXK_F13 : "WXK_F13",
68 WXK_F14 : "WXK_F14",
69 WXK_F15 : "WXK_F15",
70 WXK_F16 : "WXK_F16",
71 WXK_F17 : "WXK_F17",
72 WXK_F18 : "WXK_F18",
73 WXK_F19 : "WXK_F19",
74 WXK_F20 : "WXK_F20",
75 WXK_F21 : "WXK_F21",
76 WXK_F22 : "WXK_F22",
77 WXK_F23 : "WXK_F23",
78 WXK_F24 : "WXK_F24",
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",
110 }
111
112
113 #----------------------------------------------------------------------
114
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
121 self.logKeyDn = true
122 self.logKeyUp = true
123 self.logChar = true
124
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)
129
130 EVT_KEY_DOWN(self, self.OnKeyDown)
131 EVT_KEY_UP(self, self.OnKeyUp)
132 EVT_CHAR(self, self.OnChar)
133
134
135 def SetCallSkip(self, skip):
136 self.callSkip = skip
137
138 def SetLogKeyUp(self, val):
139 self.logKeyUp = val
140
141 def SetLogKeyDn(self, val):
142 self.logKeyDn = val
143
144 def SetLogChar(self, val):
145 self.logChar = val
146
147
148 def OnPaint(self, evt):
149 dc = wxPaintDC(self)
150 rect = self.GetClientRect()
151 dc.SetTextForeground(wxWHITE)
152 dc.DrawLabel("Click here and then press some keys",
153 rect, wxALIGN_CENTER | wxALIGN_TOP)
154 if self.haveFocus:
155 dc.SetTextForeground(wxGREEN)
156 dc.DrawLabel("Have Focus", rect, wxALIGN_RIGHT | wxALIGN_BOTTOM)
157 else:
158 dc.SetTextForeground(wxRED)
159 dc.DrawLabel("Need Focus!", rect, wxALIGN_RIGHT | wxALIGN_BOTTOM)
160
161
162 def OnSetFocus(self, evt):
163 self.haveFocus = true
164 self.Refresh()
165
166 def OnKillFocus(self, evt):
167 self.haveFocus = false
168 self.Refresh()
169
170 def OnMouse(self, evt):
171 if evt.ButtonDown():
172 self.SetFocus()
173
174
175 def OnKeyDown(self, evt):
176 if self.logKeyDn:
177 self.GetParent().keylog.LogKeyEvent("KeyDown", evt)
178 if self.callSkip:
179 evt.Skip()
180
181 def OnKeyUp(self, evt):
182 if self.logKeyUp:
183 self.GetParent().keylog.LogKeyEvent("KeyUp", evt)
184 if self.callSkip:
185 evt.Skip()
186
187 def OnChar(self, evt):
188 if self.logChar:
189 self.GetParent().keylog.LogKeyEvent("Char", evt)
190
191
192 #----------------------------------------------------------------------
193
194 from wxPython.lib.mixins.listctrl import wxListCtrlAutoWidthMixin
195
196 class KeyLog(wxListCtrl, wxListCtrlAutoWidthMixin):
197
198 def __init__(self, parent):
199 wxListCtrl.__init__(self, parent, -1,
200 style = wxLC_REPORT|wxLC_VRULES|wxLC_HRULES)
201 wxListCtrlAutoWidthMixin.__init__(self)
202
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, "")
210
211 for x in range(6):
212 self.SetColumnWidth(x, wxLIST_AUTOSIZE_USEHEADER)
213
214 self.SetColumnWidth(1, 125)
215
216
217 def LogKeyEvent(self, evType, evt):
218 keycode = evt.GetKeyCode()
219 keyname = keyMap.get(keycode, None)
220 if keyname is None:
221 if keycode < 256:
222 if keycode == 0:
223 keyname = "NUL"
224 elif keycode < 27:
225 keyname = "Ctrl-%s" % chr(ord('A') + keycode-1)
226 else:
227 keyname = "\"%s\"" % chr(keycode)
228 else:
229 keyname = "unknown (%s)" % keycode
230
231 modifiers = ""
232 for mod, ch in [(evt.ControlDown(), 'C'),
233 (evt.AltDown(), 'A'),
234 (evt.ShiftDown(), 'S'),
235 (evt.MetaDown(), 'M')]:
236 if mod:
237 modifiers += ch
238 else:
239 modifiers += '-'
240
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()))
247
248 #print ( id, evType, keyname, str(keycode), modifiers, str(evt.GetRawKeyCode()), str(evt.GetRawKeyFlags()))
249
250 self.EnsureVisible(id)
251
252
253 def ClearLog(self):
254 self.DeleteAllItems()
255
256
257 #----------------------------------------------------------------------
258
259
260 class TestPanel(wxPanel):
261 def __init__(self, parent, log):
262 self.log = 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)
267
268 btn = wxButton(self, -1, "Clear Log")
269 EVT_BUTTON(self, btn.GetId(), self.OnClearBtn)
270
271 cb1 = wxCheckBox(self, -1, "Call evt.Skip for Key Up/Dn events")
272 EVT_CHECKBOX(self, cb1.GetId(), self.OnSkipCB)
273
274 cb2 = wxCheckBox(self, -1, "EVT_KEY_UP")
275 EVT_CHECKBOX(self, cb2.GetId(), self.OnKeyUpCB)
276 cb2.SetValue(true)
277
278 cb3 = wxCheckBox(self, -1, "EVT_KEY_DOWN")
279 EVT_CHECKBOX(self, cb3.GetId(), self.OnKeyDnCB)
280 cb3.SetValue(true)
281
282 cb4 = wxCheckBox(self, -1, "EVT_CHAR")
283 EVT_CHECKBOX(self, cb4.GetId(), self.OnCharCB)
284 cb4.SetValue(true)
285
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)
292
293 sizer = wxBoxSizer(wxVERTICAL)
294 sizer.Add(self.keysink, 0, wxGROW)
295 sizer.Add(buttons)
296 sizer.Add(self.keylog, 1, wxGROW)
297
298 self.SetSizer(sizer)
299
300
301 def OnClearBtn(self, evt):
302 self.keylog.ClearLog()
303
304 def OnSkipCB(self, evt):
305 self.keysink.SetCallSkip(evt.GetInt())
306
307 def OnKeyUpCB(self, evt):
308 self.keysink.SetLogKeyUp(evt.GetInt())
309
310 def OnKeyDnCB(self, evt):
311 self.keysink.SetLogKeyDn(evt.GetInt())
312
313 def OnCharCB(self, evt):
314 self.keysink.SetLogChar(evt.GetInt())
315
316
317 #----------------------------------------------------------------------
318
319 def runTest(frame, nb, log):
320 win = TestPanel(nb, log)
321 return win
322
323 #----------------------------------------------------------------------
324
325
326
327 overview = """<html><body>
328 <h2><center>wxKeyEvents</center></h2>
329
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.
332
333 </body></html>
334 """
335
336
337
338 if __name__ == '__main__':
339 import sys,os
340 import run
341 run.main(['', os.path.basename(sys.argv[0])])
342