]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxKeyEvents.py
1 # 11/19/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
5 # 11/29/2003 - Jeff Grimmett (grimmtooth@softhome.net)
7 # o lib.mixins.listctrl needs wx renamer applied.
9 # 12/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
11 # o wxListCtrlAutoWidthMixin -> ListCtrlAutoWidthMixin
15 import wx
.lib
.mixins
.listctrl
as listmix
17 #----------------------------------------------------------------------
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",
126 #----------------------------------------------------------------------
128 class KeySink(wx
.Window
):
129 def __init__(self
, parent
):
130 wx
.Window
.__init
__(self
, parent
, -1, style
=wx
.WANTS_CHARS
135 self
.SetBackgroundColour(wx
.BLUE
)
136 self
.haveFocus
= False
137 self
.callSkip
= False
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
)
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
)
152 def SetCallSkip(self
, skip
):
155 def SetLogKeyUp(self
, val
):
158 def SetLogKeyDn(self
, val
):
161 def SetLogChar(self
, val
):
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
)
172 dc
.SetTextForeground(wx
.GREEN
)
173 dc
.DrawLabel("Have Focus", rect
, wx
.ALIGN_RIGHT | wx
.ALIGN_BOTTOM
)
175 dc
.SetTextForeground(wx
.RED
)
176 dc
.DrawLabel("Need Focus!", rect
, wx
.ALIGN_RIGHT | wx
.ALIGN_BOTTOM
)
179 def OnSetFocus(self
, evt
):
180 self
.haveFocus
= True
183 def OnKillFocus(self
, evt
):
184 self
.haveFocus
= False
187 def OnMouse(self
, evt
):
192 def OnKeyDown(self
, evt
):
194 self
.GetParent().keylog
.LogKeyEvent("KeyDown", evt
)
198 def OnKeyUp(self
, evt
):
200 self
.GetParent().keylog
.LogKeyEvent("KeyUp", evt
)
204 def OnChar(self
, evt
):
206 self
.GetParent().keylog
.LogKeyEvent("Char", evt
)
209 #----------------------------------------------------------------------
211 class KeyLog(wx
.ListCtrl
, listmix
.ListCtrlAutoWidthMixin
):
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
)
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, "")
227 self
.SetColumnWidth(x
, wx
.LIST_AUTOSIZE_USEHEADER
)
229 self
.SetColumnWidth(1, 125)
232 def LogKeyEvent(self
, evType
, evt
):
233 keycode
= evt
.GetKeyCode()
234 keyname
= keyMap
.get(keycode
, None)
240 keyname
= "Ctrl-%s" % chr(ord('A') + keycode
-1)
242 keyname
= "\"%s\"" % chr(keycode
)
244 keyname
= "unknown (%s)" % keycode
247 for mod
, ch
in [(evt
.ControlDown(), 'C'),
248 (evt
.AltDown(), 'A'),
249 (evt
.ShiftDown(), 'S'),
250 (evt
.MetaDown(), 'M')]:
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()))
263 #print ( id, evType, keyname, str(keycode), modifiers, str(evt.GetRawKeyCode()), str(evt.GetRawKeyFlags()))
265 self
.EnsureVisible(id)
269 self
.DeleteAllItems()
272 #----------------------------------------------------------------------
275 class TestPanel(wx
.Panel
):
276 def __init__(self
, parent
, 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
)
283 btn
= wx
.Button(self
, -1, "Clear Log")
284 self
.Bind(wx
.EVT_BUTTON
, self
.OnClearBtn
, btn
)
286 cb1
= wx
.CheckBox(self
, -1, "Call evt.Skip for Key Up/Dn events")
287 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSkipCB
, cb1
)
289 cb2
= wx
.CheckBox(self
, -1, "EVT_KEY_UP")
290 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnKeyUpCB
, cb2
)
293 cb3
= wx
.CheckBox(self
, -1, "EVT_KEY_DOWN")
294 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnKeyDnCB
, cb3
)
297 cb4
= wx
.CheckBox(self
, -1, "EVT_CHAR")
298 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnCharCB
, cb4
)
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)
308 sizer
= wx
.BoxSizer(wx
.VERTICAL
)
309 sizer
.Add(self
.keysink
, 0, wx
.GROW
)
311 sizer
.Add(self
.keylog
, 1, wx
.GROW
)
316 def OnClearBtn(self
, evt
):
317 self
.keylog
.ClearLog()
319 def OnSkipCB(self
, evt
):
320 self
.keysink
.SetCallSkip(evt
.GetInt())
322 def OnKeyUpCB(self
, evt
):
323 self
.keysink
.SetLogKeyUp(evt
.GetInt())
325 def OnKeyDnCB(self
, evt
):
326 self
.keysink
.SetLogKeyDn(evt
.GetInt())
328 def OnCharCB(self
, evt
):
329 self
.keysink
.SetLogChar(evt
.GetInt())
332 #----------------------------------------------------------------------
334 def runTest(frame
, nb
, log
):
335 win
= TestPanel(nb
, log
)
338 #----------------------------------------------------------------------
342 overview
= """<html><body>
343 <h2><center>wxKeyEvents</center></h2>
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.
353 if __name__
== '__main__':
356 run
.main(['', os
.path
.basename(sys
.argv
[0])])