]>
Commit | Line | Data |
---|---|---|
a660d684 KB |
1 | \section{\class{wxKeyEvent}}\label{wxkeyevent} |
2 | ||
4ce81a75 | 3 | This event class contains information about keypress (character) events. |
a660d684 | 4 | |
fc2171bd | 5 | Notice that there are three different kinds of keyboard events in wxWidgets: |
f17393f1 VZ |
6 | key down and up events and char events. The difference between the first two |
7 | is clear - the first corresponds to a key press and the second to a key | |
8 | release - otherwise they are identical. Just note that if the key is | |
9 | maintained in a pressed state you will typically get a lot of (automatically | |
68304caf | 10 | generated) down events but only one up so it is wrong to assume that there is |
f17393f1 VZ |
11 | one up event corresponding to each down one. |
12 | ||
13 | Both key events provide untranslated key codes while the char event carries | |
14 | the translated one. The untranslated code for alphanumeric keys is always | |
15 | an upper case value. For the other keys it is one of {\tt WXK\_XXX} values | |
16 | from the \helpref{keycodes table}{keycodes}. The translated key is, in | |
17 | general, the character the user expects to appear as the result of the key | |
18 | combination when typing the text into a text entry zone, for example. | |
19 | ||
20 | A few examples to clarify this (all assume that {\sc Caps Lock} is unpressed | |
21 | and the standard US keyboard): when the {\tt 'A'} key is pressed, the key down | |
22 | event key code is equal to {\tt ASCII A} $== 65$. But the char event key code | |
23 | is {\tt ASCII a} $== 97$. On the other hand, if you press both {\sc Shift} and | |
24 | {\tt 'A'} keys simultaneously , the key code in key down event will still be | |
68304caf | 25 | just {\tt 'A'} while the char event key code parameter will now be {\tt 'A'} |
f17393f1 VZ |
26 | as well. |
27 | ||
28 | Although in this simple case it is clear that the correct key code could be | |
68304caf RD |
29 | found in the key down event handler by checking the value returned by |
30 | \helpref{ShiftDown()}{wxkeyeventshiftdown}, in general you should use | |
154b6b0f | 31 | {\tt EVT\_CHAR} for this as for non-alphanumeric keys the translation is |
f17393f1 VZ |
32 | keyboard-layout dependent and can only be done properly by the system itself. |
33 | ||
34 | Another kind of translation is done when the control key is pressed: for | |
35 | example, for {\sc Ctrl-A} key press the key down event still carries the | |
36 | same key code {\tt 'a'} as usual but the char event will have key code of | |
37 | $1$, the ASCII value of this key combination. | |
38 | ||
39 | You may discover how the other keys on your system behave interactively by | |
fc2171bd | 40 | running the \helpref{text}{sampletext} wxWidgets sample and pressing some keys |
f17393f1 VZ |
41 | in any of the text controls shown in it. |
42 | ||
af79fc6f | 43 | {\bf Note:} If a key down ({\tt EVT\_KEY\_DOWN}) event is caught and |
3103e8a9 | 44 | the event handler does not call {\tt event.Skip()} then the corresponding |
af79fc6f RD |
45 | char event ({\tt EVT\_CHAR}) will not happen. This is by design and |
46 | enables the programs that handle both types of events to be a bit | |
47 | simpler. | |
48 | ||
fc2171bd | 49 | {\bf Note for Windows programmers:} The key and char events in wxWidgets are |
68304caf | 50 | similar to but slightly different from Windows {\tt WM\_KEYDOWN} and |
f17393f1 | 51 | {\tt WM\_CHAR} events. In particular, Alt-x combination will generate a char |
fc2171bd | 52 | event in wxWidgets (unless it is used as an accelerator). |
f17393f1 | 53 | |
f4fcc291 JS |
54 | {\bf Tip:} be sure to call {\tt event.Skip()} for events that you don't process in |
55 | key event function, otherwise menu shortcuts may cease to work under Windows. | |
56 | ||
a660d684 KB |
57 | \wxheading{Derived from} |
58 | ||
59 | \helpref{wxEvent}{wxevent} | |
60 | ||
954b8ae6 JS |
61 | \wxheading{Include files} |
62 | ||
63 | <wx/event.h> | |
64 | ||
a7af285d VZ |
65 | \wxheading{Library} |
66 | ||
67 | \helpref{wxCore}{librarieslist} | |
68 | ||
a660d684 KB |
69 | \wxheading{Event table macros} |
70 | ||
71 | To process a key event, use these event handler macros to direct input to member | |
72 | functions that take a wxKeyEvent argument. | |
73 | ||
74 | \twocolwidtha{7cm} | |
75 | \begin{twocollist}\itemsep=0pt | |
4ce81a75 JS |
76 | \twocolitem{{\bf EVT\_KEY\_DOWN(func)}}{Process a wxEVT\_KEY\_DOWN event (any key has been pressed).} |
77 | \twocolitem{{\bf EVT\_KEY\_UP(func)}}{Process a wxEVT\_KEY\_UP event (any key has been released).} | |
a660d684 | 78 | \twocolitem{{\bf EVT\_CHAR(func)}}{Process a wxEVT\_CHAR event.} |
f17393f1 | 79 | %\twocolitem{{\bf EVT\_CHAR\_HOOK(func)}}{Process a wxEVT\_CHAR\_HOOK event.} |
a660d684 KB |
80 | \end{twocollist}% |
81 | ||
4ce81a75 | 82 | |
a660d684 KB |
83 | \latexignore{\rtfignore{\wxheading{Members}}} |
84 | ||
0d9b2c16 | 85 | |
f0e8a2d0 | 86 | \membersection{wxKeyEvent::m\_altDown}\label{wxkeyeventmaltdown} |
a660d684 KB |
87 | |
88 | \member{bool}{m\_altDown} | |
89 | ||
e7102772 VZ |
90 | \textbf{Deprecated: } Please use \helpref{GetModifiers}{wxkeyeventgetmodifiers} |
91 | instead! | |
92 | ||
cc81d32f | 93 | true if the Alt key is pressed down. |
a660d684 | 94 | |
0d9b2c16 | 95 | |
f0e8a2d0 | 96 | \membersection{wxKeyEvent::m\_controlDown}\label{wxkeyeventmcontroldown} |
a660d684 KB |
97 | |
98 | \member{bool}{m\_controlDown} | |
99 | ||
e7102772 VZ |
100 | \textbf{Deprecated: } Please use \helpref{GetModifiers}{wxkeyeventgetmodifiers} |
101 | instead! | |
102 | ||
cc81d32f | 103 | true if control is pressed down. |
a660d684 | 104 | |
0d9b2c16 | 105 | |
f0e8a2d0 | 106 | \membersection{wxKeyEvent::m\_keyCode}\label{wxkeyeventmkeycode} |
a660d684 KB |
107 | |
108 | \member{long}{m\_keyCode} | |
109 | ||
e7102772 VZ |
110 | \textbf{Deprecated: } Please use \helpref{GetKeyCode}{wxkeyeventgetkeycode} |
111 | instead! | |
112 | ||
3972fb49 | 113 | Virtual keycode. See \helpref{Keycodes}{keycodes} for a list of identifiers. |
a660d684 | 114 | |
0d9b2c16 | 115 | |
f0e8a2d0 | 116 | \membersection{wxKeyEvent::m\_metaDown}\label{wxkeyeventmmetadown} |
a660d684 KB |
117 | |
118 | \member{bool}{m\_metaDown} | |
119 | ||
e7102772 VZ |
120 | \textbf{Deprecated: } Please use \helpref{GetModifiers}{wxkeyeventgetmodifiers} |
121 | instead! | |
122 | ||
cc81d32f | 123 | true if the Meta key is pressed down. |
a660d684 | 124 | |
0d9b2c16 | 125 | |
f0e8a2d0 | 126 | \membersection{wxKeyEvent::m\_shiftDown}\label{wxkeyeventmshiftdown} |
a660d684 KB |
127 | |
128 | \member{bool}{m\_shiftDown} | |
129 | ||
e7102772 VZ |
130 | \textbf{Deprecated: } Please use \helpref{GetModifiers}{wxkeyeventgetmodifiers} |
131 | instead! | |
132 | ||
cc81d32f | 133 | true if shift is pressed down. |
a660d684 | 134 | |
0d9b2c16 | 135 | |
f0e8a2d0 | 136 | \membersection{wxKeyEvent::m\_x}\label{wxkeyeventmx} |
a660d684 KB |
137 | |
138 | \member{int}{m\_x} | |
139 | ||
e7102772 VZ |
140 | \textbf{Deprecated: } Please use \helpref{GetX}{wxkeyeventgetx} instead! |
141 | ||
a660d684 KB |
142 | X position of the event. |
143 | ||
0d9b2c16 | 144 | |
f0e8a2d0 | 145 | \membersection{wxKeyEvent::m\_y}\label{wxkeyeventmy} |
a660d684 KB |
146 | |
147 | \member{int}{m\_y} | |
148 | ||
e7102772 VZ |
149 | \textbf{Deprecated: } Please use \helpref{GetY}{wxkeyeventgety} instead! |
150 | ||
a660d684 KB |
151 | Y position of the event. |
152 | ||
0d9b2c16 | 153 | |
f0e8a2d0 | 154 | \membersection{wxKeyEvent::wxKeyEvent}\label{wxkeyeventctor} |
a660d684 KB |
155 | |
156 | \func{}{wxKeyEvent}{\param{WXTYPE}{ keyEventType}} | |
157 | ||
158 | Constructor. Currently, the only valid event types are wxEVT\_CHAR and wxEVT\_CHAR\_HOOK. | |
159 | ||
0d9b2c16 | 160 | |
f0e8a2d0 | 161 | \membersection{wxKeyEvent::AltDown}\label{wxkeyeventaltdown} |
a660d684 | 162 | |
803ef874 | 163 | \constfunc{bool}{AltDown}{\void} |
a660d684 | 164 | |
cc81d32f | 165 | Returns true if the Alt key was down at the time of the key event. |
a660d684 | 166 | |
e7102772 VZ |
167 | Notice that \helpref{GetModifiers}{wxkeyeventgetmodifiers} is easier to use |
168 | correctly than this function so you should consider using it in new code. | |
169 | ||
0d9b2c16 | 170 | |
a2bd1520 VZ |
171 | \membersection{wxKeyEvent::CmdDown}\label{wxkeyeventcmddown} |
172 | ||
173 | \constfunc{bool}{CmdDown}{\void} | |
174 | ||
e7102772 VZ |
175 | \textsc{Cmd} is a pseudo key which is the same as Control for PC and Unix |
176 | platforms but the special \textsc{Apple} (a.k.a as \textsc{Command}) key under | |
177 | Macs: it makes often sense to use it instead of, say, ControlDown() because Cmd | |
178 | key is used for the same thing under Mac as Ctrl elsewhere (but Ctrl still | |
179 | exists, just not used for this purpose under Mac). So for non-Mac platforms | |
180 | this is the same as \helpref{ControlDown()}{wxkeyeventcontroldown} and under | |
181 | Mac this is the same as \helpref{MetaDown()}{wxkeyeventmetadown}. | |
a2bd1520 VZ |
182 | |
183 | ||
184 | \membersection{wxKeyEvent::ControlDown}\label{wxkeyeventcontroldown} | |
a660d684 | 185 | |
803ef874 | 186 | \constfunc{bool}{ControlDown}{\void} |
a660d684 | 187 | |
cc81d32f | 188 | Returns true if the control key was down at the time of the key event. |
a660d684 | 189 | |
e7102772 VZ |
190 | Notice that \helpref{GetModifiers}{wxkeyeventgetmodifiers} is easier to use |
191 | correctly than this function so you should consider using it in new code. | |
192 | ||
0d9b2c16 | 193 | |
f0e8a2d0 | 194 | \membersection{wxKeyEvent::GetKeyCode}\label{wxkeyeventgetkeycode} |
f6bcfd97 BP |
195 | |
196 | \constfunc{int}{GetKeyCode}{\void} | |
197 | ||
198 | Returns the virtual key code. ASCII events return normal ASCII values, | |
199 | while non-ASCII events return values such as {\bf WXK\_LEFT} for the | |
67aa6c1e VS |
200 | left cursor key. See \helpref{Keycodes}{keycodes} for a full list of |
201 | the virtual key codes. | |
202 | ||
203 | Note that in Unicode build, the returned value is meaningful only if the | |
204 | user entered a character that can be represented in current locale's default | |
205 | charset. You can obtain the corresponding Unicode character using | |
206 | \helpref{GetUnicodeKey}{wxkeyeventgetunicodekey}. | |
f6bcfd97 | 207 | |
0d9b2c16 | 208 | |
e7102772 VZ |
209 | \membersection{wxKeyEvent::GetModifiers}\label{wxkeyeventgetmodifiers} |
210 | ||
211 | \constfunc{int}{GetModifiers}{\void} | |
212 | ||
213 | Return the bitmask of modifier keys which were pressed when this event | |
214 | happened. See \helpref{key modifier constants}{keymodifiers} for the full list | |
215 | of modifiers. | |
216 | ||
217 | Notice that this function is easier to use correctly than, for example, | |
218 | \helpref{ControlDown}{wxkeyeventcontroldown} because when using the latter you | |
219 | also have to remember to test that none of the other modifiers is pressed: | |
220 | ||
221 | \begin{verbatim} | |
222 | if ( ControlDown() && !AltDown() && !ShiftDown() && !MetaDown() ) | |
223 | ... handle Ctrl-XXX ... | |
224 | \end{verbatim} | |
225 | ||
226 | and forgetting to do it can result in serious program bugs (e.g. program not | |
227 | working with European keyboard layout where \textsc{AltGr} key which is seen by | |
228 | the program as combination of \textsc{Ctrl} and \textsc{Alt} is used). On the | |
229 | other hand, you can simply write | |
230 | ||
231 | \begin{verbatim} | |
232 | if ( GetModifiers() == wxMOD_CONTROL ) | |
233 | ... handle Ctrl-XXX ... | |
234 | \end{verbatim} | |
235 | ||
236 | with this function. | |
237 | ||
238 | ||
f0e8a2d0 | 239 | \membersection{wxKeyEvent::GetPosition}\label{wxkeyeventgetposition} |
0d9b2c16 VZ |
240 | |
241 | \constfunc{wxPoint}{GetPosition}{\void} | |
242 | ||
243 | \constfunc{void}{GetPosition}{\param{long *}{x}, \param{long *}{y}} | |
244 | ||
245 | Obtains the position (in client coordinates) at which the key was pressed. | |
246 | ||
247 | ||
f0e8a2d0 | 248 | \membersection{wxKeyEvent::GetRawKeyCode}\label{wxkeyeventgetrawkeycode} |
a160a325 VZ |
249 | |
250 | \constfunc{wxUint32}{GetRawKeyCode}{\void} | |
251 | ||
252 | Returns the raw key code for this event. This is a platform-dependent scan code | |
253 | which should only be used in advanced applications. | |
254 | ||
255 | {\bf NB:} Currently the raw key codes are not supported by all ports, use | |
7af3ca16 | 256 | {\tt\#ifdef wxHAS\_RAW\_KEY\_CODES} to determine if this feature is available. |
a160a325 | 257 | |
0d9b2c16 | 258 | |
f0e8a2d0 | 259 | \membersection{wxKeyEvent::GetRawKeyFlags}\label{wxkeyeventgetrawkeyflags} |
a160a325 VZ |
260 | |
261 | \constfunc{wxUint32}{GetRawKeyFlags}{\void} | |
262 | ||
263 | Returns the low level key flags for this event. The flags are | |
264 | platform-dependent and should only be used in advanced applications. | |
265 | ||
266 | {\bf NB:} Currently the raw key flags are not supported by all ports, use | |
7af3ca16 | 267 | {\tt \#ifdef wxHAS\_RAW\_KEY\_CODES} to determine if this feature is available. |
a160a325 | 268 | |
a660d684 | 269 | |
f0e8a2d0 | 270 | \membersection{wxKeyEvent::GetUnicodeKey}\label{wxkeyeventgetunicodekey} |
a660d684 | 271 | |
0d9b2c16 | 272 | \constfunc{wxChar}{GetUnicodeKey}{\void} |
a660d684 | 273 | |
0d9b2c16 | 274 | Returns the Unicode character corresponding to this key event. |
a660d684 | 275 | |
0d9b2c16 VZ |
276 | This function is only available in Unicode build, i.e. when |
277 | \texttt{wxUSE\_UNICODE} is $1$. | |
a660d684 | 278 | |
a660d684 | 279 | |
f0e8a2d0 | 280 | \membersection{wxKeyEvent::GetX}\label{wxkeyeventgetx} |
a660d684 | 281 | |
0d9b2c16 | 282 | \constfunc{long}{GetX}{\void} |
a660d684 | 283 | |
0d9b2c16 | 284 | Returns the X position (in client coordinates) of the event. |
a660d684 | 285 | |
a660d684 | 286 | |
f0e8a2d0 | 287 | \membersection{wxKeyEvent::GetY}\label{wxkeyeventgety} |
803ef874 | 288 | |
0d9b2c16 VZ |
289 | \constfunc{long}{GetY}{\void} |
290 | ||
291 | Returns the Y (in client coordinates) position of the event. | |
a660d684 | 292 | |
a660d684 | 293 | |
f0e8a2d0 | 294 | \membersection{wxKeyEvent::HasModifiers}\label{wxkeyeventhasmodifiers} |
f6bcfd97 BP |
295 | |
296 | \constfunc{bool}{HasModifiers}{\void} | |
297 | ||
cc81d32f | 298 | Returns true if either {\sc Ctrl} or {\sc Alt} keys was down |
f6bcfd97 | 299 | at the time of the key event. Note that this function does not take into |
d11710cb | 300 | account neither {\sc Shift} nor {\sc Meta} key states (the reason for ignoring |
68304caf | 301 | the latter is that it is common for {\sc NumLock} key to be configured as |
d11710cb VZ |
302 | {\sc Meta} under X but the key presses even while {\sc NumLock} is on should |
303 | be still processed normally). | |
f6bcfd97 | 304 | |
0d9b2c16 | 305 | |
a2bd1520 | 306 | \membersection{wxKeyEvent::MetaDown}\label{wxkeyeventmetadown} |
0d9b2c16 VZ |
307 | |
308 | \constfunc{bool}{MetaDown}{\void} | |
309 | ||
310 | Returns true if the Meta key was down at the time of the key event. | |
311 | ||
e7102772 VZ |
312 | Notice that \helpref{GetModifiers}{wxkeyeventgetmodifiers} is easier to use |
313 | correctly than this function so you should consider using it in new code. | |
314 | ||
0d9b2c16 | 315 | |
f17393f1 | 316 | \membersection{wxKeyEvent::ShiftDown}\label{wxkeyeventshiftdown} |
a660d684 | 317 | |
803ef874 | 318 | \constfunc{bool}{ShiftDown}{\void} |
a660d684 | 319 | |
cc81d32f | 320 | Returns true if the shift key was down at the time of the key event. |
a660d684 | 321 | |
e7102772 VZ |
322 | Notice that \helpref{GetModifiers}{wxkeyeventgetmodifiers} is easier to use |
323 | correctly than this function so you should consider using it in new code. | |
324 |