1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
|
.. wxPython Phoenix documentation
This file was generated by Phoenix's sphinx generator and associated
tools, do not edit by hand.
Copyright: (c) 2011-2020 by Total Control Software
License: wxWindows License
.. include:: headings.inc
.. _wx.KeyEvent:
==========================================================================================================================================
|phoenix_title| **wx.KeyEvent**
==========================================================================================================================================
This event class contains information about key press and release events.
The main information carried by this event is the key being pressed or released. It can be accessed using one of :meth:`~wx.KeyEvent.GetUnicodeKey`, :meth:`~wx.KeyEvent.GetKeyCode` or :meth:`~wx.KeyEvent.GetRawKeyCode` functions. For the printable characters, :meth:`~wx.KeyEvent.GetUnicodeKey` should be used as it works for any keys, including non-Latin-1 characters that can be entered when using national keyboard layouts. :meth:`~wx.KeyEvent.GetKeyCode` should be used to handle special characters (such as cursor arrows keys or ``HOME`` or ``INS`` and so on) which correspond to :ref:`wx.KeyCode` enum elements above the ``WXK_START`` constant. While :meth:`~wx.KeyEvent.GetKeyCode` also returns the character code for Latin-1 keys for compatibility, it doesn't work for Unicode characters in general and will return ``WXK_NONE`` for any non-Latin-1 ones. If both :meth:`~wx.KeyEvent.GetUnicodeKey` and :meth:`~wx.KeyEvent.GetKeyCode` return ``WXK_NONE`` then the key has no ``WXK_xxx`` mapping and :meth:`~wx.KeyEvent.GetRawKeyCode` can be used to distinguish between keys, but raw key codes are platform specific. For these reasons, it is recommended to always use :meth:`~wx.KeyEvent.GetUnicodeKey` and only fall back to :meth:`~wx.KeyEvent.GetKeyCode` if :meth:`~wx.KeyEvent.GetUnicodeKey` returned ``WXK_NONE`` , meaning that the event corresponds to a non-printable special keys, then optionally check :meth:`~wx.KeyEvent.GetRawKeyCode` if :meth:`~wx.KeyEvent.GetKeyCode` also returned ``WXK_NONE`` or simply ignore that key.
While these three functions can be used with the events of ``wxEVT_KEY_DOWN`` , ``wxEVT_KEY_UP`` and ``wxEVT_CHAR`` types, the values returned by them are different for the first two events and the last one. For the latter, the key returned corresponds to the character that would appear in e.g. a text zone if the user pressed the key in it. As such, its value depends on the current state of the Shift key and, for the letters, on the state of Caps Lock modifier. For example, if ``A`` key is pressed without Shift being held down, :ref:`wx.KeyEvent` of type ``wxEVT_CHAR`` generated for this key press will return (from either :meth:`~wx.KeyEvent.GetKeyCode` or :meth:`~wx.KeyEvent.GetUnicodeKey` as their meanings coincide for ``ASCII`` characters) key code of 97 corresponding the ``ASCII`` value of ``a`` . And if the same key is pressed but with Shift being held (or Caps Lock being active), then the key could would be 65, i.e. ``ASCII`` value of capital ``A`` .
However for the key down and up events the returned key code will instead be ``A`` independently of the state of the modifier keys i.e. it depends only on physical key being pressed and is not translated to its logical representation using the current keyboard state. Such untranslated key codes are defined as follows:
- For the letters they correspond to the `upper` case value of the letter.
- For the other alphanumeric keys (e.g. ``7`` or ``+`` ), the untranslated key code corresponds to the character produced by the key when it is pressed without Shift. E.g. in standard ``US`` keyboard layout the untranslated key code for the key ``=/+`` in the upper right corner of the keyboard is 61 which is the ``ASCII`` value of ``=`` .
- For the rest of the keys (i.e. special non-printable keys) it is the same as the normal key code as no translation is used anyhow.
Notice that the first rule applies to all Unicode letters, not just the usual Latin-1 ones. However for non-Latin-1 letters only :meth:`~wx.KeyEvent.GetUnicodeKey` can be used to retrieve the key code as :meth:`~wx.KeyEvent.GetKeyCode` just returns ``WXK_NONE`` in this case.
Also, note that ``wxEVT_CHAR`` events are not generated for keys which do not have a wxWidgets mapping, so :meth:`~wx.KeyEvent.GetRawKeyCode` should never be required for this event.
To summarize: you should handle ``wxEVT_CHAR`` if you need the translated key and ``wxEVT_KEY_DOWN`` if you only need the value of the key itself, independent of the current keyboard state.
Another difference between key and int events is that another kind of translation is done for the latter ones when the Control key is pressed: int events for ``ASCII`` letters in this case carry codes corresponding to the ``ASCII`` value of Ctrl-Latter, i.e. 1 for Ctrl-A, 2 for Ctrl-B and so on until 26 for Ctrl-Z. This is convenient for terminal-like applications and can be completely ignored by all the other ones (if you need to handle Ctrl-A it is probably a better idea to use the key event rather than the int one). Notice that currently no translation is done for the presses of ``\`` , ``^`` and ``_`` keys which might be mapped to ``ASCII`` values from 27 to 31. Since version 2.9.2, the enum values ``WXK_CONTROL_A`` - ``WXK_CONTROL_Z`` can be used instead of the non-descriptive constant values ``1-26``.
Finally, modifier keys only generate key events but no int events at all. The modifiers keys are ``WXK_SHIFT`` , ``WXK_CONTROL`` , ``WXK_ALT`` and various ``WXK_WINDOWS_XXX`` from :ref:`wx.KeyCode` enum.
Modifier keys events are special in one additional aspect: usually the keyboard state associated with a key press is well defined, e.g. :meth:`wx.KeyboardState.ShiftDown` returns ``true`` only if the Shift key was held pressed when the key that generated this event itself was pressed. There is an ambiguity for the key press events for Shift key itself however. By convention, it is considered to be already pressed when it is pressed and already released when it is released. In other words, ``wxEVT_KEY_DOWN`` event for the Shift key itself will have ``MOD_SHIFT`` in :meth:`~wx.KeyEvent.GetModifiers` and :meth:`~wx.KeyEvent.ShiftDown` will return ``True`` while the ``wxEVT_KEY_UP`` event for Shift itself will not have ``MOD_SHIFT`` in its modifiers and :meth:`~wx.KeyEvent.ShiftDown` will return ``False``.
**Tip:** You may discover the key codes and modifiers generated by all the keys on your system interactively by running the :ref:`Key Event Sample <key event sample>` wxWidgets sample and pressing some keys in it.
^^
.. _KeyEvent-events:
|events| Events Emitted by this Class
=====================================
Handlers bound for the following event types will receive a :ref:`wx.KeyEvent` parameter.
Event macros:
- EVT_KEY_DOWN: Process a ``wxEVT_KEY_DOWN`` event (any key has been pressed). If this event is handled and not skipped, ``wxEVT_CHAR`` will not be generated at all for this key press (but ``wxEVT_KEY_UP`` will be).
- EVT_KEY_UP: Process a ``wxEVT_KEY_UP`` event (any key has been released).
- EVT_CHAR: Process a ``wxEVT_CHAR`` event.
- EVT_CHAR_HOOK: Process a ``wxEVT_CHAR_HOOK`` event. Unlike all the other key events, this event is propagated upwards the window hierarchy which allows intercepting it in the parent window of the focused window to which it is sent initially (if there is no focused window, this event is sent to the :ref:`wx.App` global object). It is also generated before any other key events and so gives the parent window an opportunity to modify the keyboard handling of its children, e.g. it is used internally by wxWidgets in some ports to intercept pressing Esc key in any child of a dialog to close the dialog itself when it's pressed. By default, if this event is handled, i.e. the handler doesn't call :meth:`wx.Event.Skip` , neither ``wxEVT_KEY_DOWN`` nor ``wxEVT_CHAR`` events will be generated (although ``wxEVT_KEY_UP`` still will be), i.e. it replaces the normal key events. However by calling the special :meth:`~wx.KeyEvent.DoAllowNextEvent` method you can handle ``wxEVT_CHAR_HOOK`` and still allow normal events generation. This is something that is rarely useful but can be required if you need to prevent a parent ``wxEVT_CHAR_HOOK`` handler from running without suppressing the normal key events. Finally notice that this event is not generated when the mouse is captured as it is considered that the window which has the capture should receive all the keyboard events too without allowing its parent :ref:`wx.TopLevelWindow` to interfere with their processing. ^^
.. note::
Not all key down events may be generated by the user. As an example, ``wxEVT_KEY_DOWN`` with ``=`` key code can be generated using the standard ``US`` keyboard layout but not using the German one because the ``=`` key corresponds to Shift-0 key combination in this layout and the key code for it is ``0`` , not ``=`` . Because of this you should avoid requiring your users to type key events that might be impossible to enter on their keyboard.
.. note::
If a key down ( ``EVT_KEY_DOWN`` ) event is caught and the event handler does not call ``event.Skip()`` then the corresponding int event ( ``EVT_CHAR`` ) will not happen. This is by design and enables the programs that handle both types of events to avoid processing the same key twice. As a consequence, if you do not want to suppress the ``wxEVT_CHAR`` events for the keys you handle, always call ``event.Skip()`` in your ``wxEVT_KEY_DOWN`` handler. Not doing may also prevent accelerators defined using this key from working.
.. note::
If a key is maintained in a pressed state, you will typically get a lot of (automatically generated) key down events but only one key up one at the end when the key is released so it is wrong to assume that there is one up event corresponding to each down one.
.. note::
For Windows programmers: The key and int events in wxWidgets are similar to but slightly different from Windows ``WM_KEYDOWN`` and ``WM_CHAR`` events. In particular, Alt-x combination will generate a int event in wxWidgets (unless it is used as an accelerator) and almost all keys, including ones without ``ASCII`` equivalents, generate int events too.
.. seealso:: :ref:`wx.KeyboardState`
|
|class_hierarchy| Class Hierarchy
=================================
.. raw:: html
<div id="toggleBlock" onclick="return toggleVisibility(this)" class="closed" style="cursor:pointer;">
<img id="toggleBlock-trigger" src="_static/images/closed.png"/>
Inheritance diagram for class <strong>KeyEvent</strong>:
</div>
<div id="toggleBlock-summary" style="display:block;"></div>
<div id="toggleBlock-content" style="display:none;">
<p class="graphviz">
<center><img src="_static/images/inheritance/wx.KeyEvent_inheritance.png" alt="Inheritance diagram of KeyEvent" usemap="#dummy" class="inheritance"/></center>
<script type="text/javascript">toggleVisibilityOnLoad(document.getElementById('toggleBlock'))</script>
<map id="dummy" name="dummy"> <area shape="rect" id="node1" href="wx.KeyEvent.html" title="wx.KeyEvent" alt="" coords="63,160,170,189"/> <area shape="rect" id="node2" href="wx.Event.html" title="wx.Event" alt="" coords="7,83,90,112"/> <area shape="rect" id="node3" href="wx.KeyboardState.html" title="wx.KeyboardState" alt="" coords="115,83,255,112"/> <area shape="rect" id="node4" href="wx.Object.html" title="wx.Object" alt="" coords="5,5,92,35"/> </map>
</p>
</div>
|
|method_summary| Methods Summary
================================
================================================================================ ================================================================================
:meth:`~wx.KeyEvent.__init__` Constructor.
:meth:`~wx.KeyEvent.DoAllowNextEvent` Allow normal key events generation.
:meth:`~wx.KeyEvent.GetKeyCode` Returns the key code of the key that generated this event.
:meth:`~wx.KeyEvent.GetPosition` Obtains the position (in client coordinates) at which the key was pressed.
:meth:`~wx.KeyEvent.GetRawKeyCode` Returns the raw key code for this event.
:meth:`~wx.KeyEvent.GetRawKeyFlags` Returns the low level key flags for this event.
:meth:`~wx.KeyEvent.GetUnicodeKey` Returns the Unicode character corresponding to this key event.
:meth:`~wx.KeyEvent.GetX` Returns the X position (in client coordinates) of the event.
:meth:`~wx.KeyEvent.GetY` Returns the Y position (in client coordinates) of the event.
:meth:`~wx.KeyEvent.IsAutoRepeat` Returns ``True`` if this event is an auto-repeat of the key, ``False`` if this is the initial key press.
:meth:`~wx.KeyEvent.IsKeyInCategory` Returns ``True`` if the key is in the given key category.
:meth:`~wx.KeyEvent.IsNextEventAllowed` Returns ``True`` if :meth:`~KeyEvent.DoAllowNextEvent` had been called, ``False`` by default.
:meth:`~wx.KeyEvent.SetKeyCode`
:meth:`~wx.KeyEvent.SetRawKeyCode`
:meth:`~wx.KeyEvent.SetRawKeyFlags`
:meth:`~wx.KeyEvent.SetUnicodeKey`
================================================================================ ================================================================================
|
|property_summary| Properties Summary
=====================================
================================================================================ ================================================================================
:attr:`~wx.KeyEvent.KeyCode` See :meth:`~wx.KeyEvent.GetKeyCode` and :meth:`~wx.KeyEvent.SetKeyCode`
:attr:`~wx.KeyEvent.Position` See :meth:`~wx.KeyEvent.GetPosition`
:attr:`~wx.KeyEvent.RawKeyCode` See :meth:`~wx.KeyEvent.GetRawKeyCode` and :meth:`~wx.KeyEvent.SetRawKeyCode`
:attr:`~wx.KeyEvent.RawKeyFlags` See :meth:`~wx.KeyEvent.GetRawKeyFlags` and :meth:`~wx.KeyEvent.SetRawKeyFlags`
:attr:`~wx.KeyEvent.UnicodeKey` See :meth:`~wx.KeyEvent.GetUnicodeKey` and :meth:`~wx.KeyEvent.SetUnicodeKey`
:attr:`~wx.KeyEvent.X` See :meth:`~wx.KeyEvent.GetX`
:attr:`~wx.KeyEvent.Y` See :meth:`~wx.KeyEvent.GetY`
================================================================================ ================================================================================
|
|api| Class API
===============
.. class:: wx.KeyEvent(Event, KeyboardState)
**Possible constructors**::
KeyEvent(keyEventType=wxEVT_NULL)
This event class contains information about key press and release
events.
.. method:: __init__(self, keyEventType=wxEVT_NULL)
Constructor.
Currently, the only valid event types are ``wxEVT_CHAR`` and ``wxEVT_CHAR_HOOK`` .
:param `keyEventType`:
:type `keyEventType`: wx.EventType
.. method:: DoAllowNextEvent(self)
Allow normal key events generation.
Can be called from ``wxEVT_CHAR_HOOK`` handler to indicate that the generation of normal events should `not` be suppressed, as it happens by default when this event is handled.
The intended use of this method is to allow some window object to prevent ``wxEVT_CHAR_HOOK`` handler in its parent window from running by defining its own handler for this event. Without calling this method, this would result in not generating ``wxEVT_KEY_DOWN`` nor ``wxEVT_CHAR`` events at all but by calling it you can ensure that these events would still be generated, even if ``wxEVT_CHAR_HOOK`` event was handled.
.. versionadded:: 2.9.3
.. method:: GetKeyCode(self)
Returns the key code of the key that generated this event.
``ASCII`` symbols return normal ``ASCII`` values, while events from special keys such as "left cursor arrow" ( ``WXK_LEFT`` ) return values outside of the ``ASCII`` range. See :ref:`wx.KeyCode` for a full list of the virtual key codes.
Note that this method returns a meaningful value only for special non-alphanumeric keys or if the user entered a Latin-1 character (this includes ``ASCII`` and the accented letters found in Western European languages but not letters of other alphabets such as e.g. Cyrillic). Otherwise it simply method returns ``WXK_NONE`` and :meth:`GetUnicodeKey` should be used to obtain the corresponding Unicode character.
Using :meth:`GetUnicodeKey` is in general the right thing to do if you are interested in the characters typed by the user, :meth:`GetKeyCode` should be only used for special keys (for which :meth:`GetUnicodeKey` returns ``WXK_NONE`` ). To handle both kinds of keys you might write: ::
def OnChar(self, event):
keycode = event.GetUnicodeKey()
if keycode != wx.WXK_NONE:
# It's a printable character
wx.LogMessage("You pressed '%c'"%keycode)
else:
# It's a special key, deal with all the known ones:
keycode = event.GetKeyCode()
if keycode in [wx.WXK_LEFT, wx.WXK_RIGHT]:
# move cursor ...
MoveCursor()
elif keycode == wx.WXK_F1:
# give help ...
GiveHelp()
:rtype: `int`
.. method:: GetPosition(self)
Obtains the position (in client coordinates) at which the key was pressed.
Notice that under most platforms this position is simply the current mouse pointer position and has no special relationship to the key event itself.
`x` and `y` may be ``None`` if the corresponding coordinate is not needed.
:rtype: :ref:`wx.Point`
.. method:: GetRawKeyCode(self)
Returns the raw key code for this event.
The flags are platform-dependent and should only be used if the functionality provided by other :ref:`wx.KeyEvent` methods is insufficient.
Under MSW, the raw key code is the value of ``wParam`` parameter of the corresponding message.
Under GTK, the raw key code is the ``keyval`` field of the corresponding ``GDK`` event.
Under macOS, the raw key code is the ``keyCode`` field of the corresponding NSEvent.
:rtype: :ref:`wx.int`
.. note::
Currently the raw key codes are not supported by all ports, use #ifdef ``HAS_RAW_KEY_CODES`` to determine if this feature is available.
.. method:: GetRawKeyFlags(self)
Returns the low level key flags for this event.
The flags are platform-dependent and should only be used if the functionality provided by other :ref:`wx.KeyEvent` methods is insufficient.
Under MSW, the raw flags are just the value of ``lParam`` parameter of the corresponding message.
Under GTK, the raw flags contain the ``hardware_keycode`` field of the corresponding ``GDK`` event.
Under macOS, the raw flags contain the modifiers state.
:rtype: :ref:`wx.int`
.. note::
Currently the raw key flags are not supported by all ports, use #ifdef ``HAS_RAW_KEY_CODES`` to determine if this feature is available.
.. method:: GetUnicodeKey(self)
Returns the Unicode character corresponding to this key event.
If the key pressed doesn't have any character value (e.g. a cursor key) this method will return ``WXK_NONE`` . In this case you should use :meth:`GetKeyCode` to retrieve the value of the key.
This function is only available in Unicode build, i.e. when ``USE_UNICODE`` is 1.
:rtype: `int`
.. method:: GetX(self)
Returns the X position (in client coordinates) of the event.
:rtype: :ref:`wx.Coord`
.. seealso:: :meth:`GetPosition`
.. method:: GetY(self)
Returns the Y position (in client coordinates) of the event.
:rtype: :ref:`wx.Coord`
.. seealso:: :meth:`GetPosition`
.. method:: IsAutoRepeat(self)
Returns ``True`` if this event is an auto-repeat of the key, ``False`` if this is the initial key press.
:rtype: `bool`
.. availability:: Only available for OSX, MSW, ``QT``.
.. method:: IsKeyInCategory(self, category)
Returns ``True`` if the key is in the given key category.
:param `category`: A bitwise combination of named :ref:`wx.KeyCategoryFlags` constants.
:type `category`: int
:rtype: `bool`
.. versionadded:: 2.9.1
.. method:: IsNextEventAllowed(self)
Returns ``True`` if :meth:`DoAllowNextEvent` had been called, ``False`` by default.
This method is used by wxWidgets itself to determine whether the normal key events should be generated after ``wxEVT_CHAR_HOOK`` processing.
:rtype: `bool`
.. versionadded:: 2.9.3
.. method:: SetKeyCode(self, keyCode)
.. method:: SetRawKeyCode(self, rawKeyCode)
.. method:: SetRawKeyFlags(self, rawFlags)
.. method:: SetUnicodeKey(self, uniChar)
.. attribute:: KeyCode
See :meth:`~wx.KeyEvent.GetKeyCode` and :meth:`~wx.KeyEvent.SetKeyCode`
.. attribute:: Position
See :meth:`~wx.KeyEvent.GetPosition`
.. attribute:: RawKeyCode
See :meth:`~wx.KeyEvent.GetRawKeyCode` and :meth:`~wx.KeyEvent.SetRawKeyCode`
.. attribute:: RawKeyFlags
See :meth:`~wx.KeyEvent.GetRawKeyFlags` and :meth:`~wx.KeyEvent.SetRawKeyFlags`
.. attribute:: UnicodeKey
See :meth:`~wx.KeyEvent.GetUnicodeKey` and :meth:`~wx.KeyEvent.SetUnicodeKey`
.. attribute:: X
See :meth:`~wx.KeyEvent.GetX`
.. attribute:: Y
See :meth:`~wx.KeyEvent.GetY`
|