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