| 1 |
.. _guide_keyboard-input: |
| 2 |
|
| 3 |
Keyboard input |
| 4 |
============== |
| 5 |
|
| 6 |
pyglet provides multiple types of keyboard input abstraction: |
| 7 |
|
| 8 |
#. Cross-platform key press/release events suitable for game controls |
| 9 |
#. Unicode text entry with automatic locale and platform handling |
| 10 |
#. Cross-platform detection of common text editing actions |
| 11 |
|
| 12 |
|
| 13 |
All of them have the following restrictions: |
| 14 |
|
| 15 |
#. There must be at least one pyglet :py:class:`~pyglet.window.Window` |
| 16 |
instance which can hold keyboard focus |
| 17 |
|
| 18 |
#. Windows created with the following styles cannot hold keyboard focus: |
| 19 |
|
| 20 |
* :py:attr:`~pyglet.window.Window.WINDOW_STYLE_BORDERLESS` |
| 21 |
* :py:attr:`~pyglet.window.Window.WINDOW_STYLE_TOOL` |
| 22 |
|
| 23 |
|
| 24 |
If your project's requirements fall outside these restrictions, you |
| 25 |
should consider alternatives. Examples include: |
| 26 |
|
| 27 |
* Python's built-in :py:func:`input` function |
| 28 |
* The `Textual <https://www.textualize.io/projects/#textual>`_ terminal UI |
| 29 |
framework |
| 30 |
|
| 31 |
|
| 32 |
Keyboard Focus Conventions |
| 33 |
-------------------------- |
| 34 |
|
| 35 |
Keyboard focus is where the user's keyboard input is sent. |
| 36 |
|
| 37 |
Desktop operating systems often follow these conventions: |
| 38 |
|
| 39 |
#. Only one window can have focus |
| 40 |
#. Clicking a window gives it focus |
| 41 |
#. The window with focus is displayed above all others |
| 42 |
#. The window with focus has a distinct title bar style |
| 43 |
#. Windows can have focus taken away |
| 44 |
#. Windows can request focus |
| 45 |
|
| 46 |
However, the items above are not guaranteed to be true. |
| 47 |
|
| 48 |
For example, pyglet allows you to request focus from the OS by calling |
| 49 |
:py:meth:`Window.activate <pyglet.window.Window.activate>`. However, |
| 50 |
the OS may not support the feature. Even if it does support it, the OS |
| 51 |
may not only refuse, but do so without notifying the user focus was |
| 52 |
requested. |
| 53 |
|
| 54 |
Deviations from the conventions can occur for any of the following |
| 55 |
reasons: |
| 56 |
|
| 57 |
.. list-table:: |
| 58 |
:header-rows: 1 |
| 59 |
|
| 60 |
* - Cause |
| 61 |
- Example(s) |
| 62 |
|
| 63 |
* - Modal dialogs |
| 64 |
- Permission requests and error notifications |
| 65 |
|
| 66 |
* - User settings |
| 67 |
- Window focus is set to follow the mouse |
| 68 |
|
| 69 |
* - Platform quirks |
| 70 |
- Split screen utilities and Linux window managers |
| 71 |
with multi-focus modes |
| 72 |
|
| 73 |
|
| 74 |
Keyboard events |
| 75 |
--------------- |
| 76 |
|
| 77 |
The :py:meth:`~pyglet.window.Window.on_key_press` and |
| 78 |
:py:meth:`~pyglet.window.Window.on_key_release` events are fired when |
| 79 |
any key on the keyboard is pressed or released, respectively. These events |
| 80 |
are not affected by "key repeat" -- once a key is pressed there are no more |
| 81 |
events for that key until it is released. |
| 82 |
|
| 83 |
Both events are parameterised by the same arguments:: |
| 84 |
|
| 85 |
def on_key_press(symbol, modifiers): |
| 86 |
pass |
| 87 |
|
| 88 |
def on_key_release(symbol, modifiers): |
| 89 |
pass |
| 90 |
|
| 91 |
Defined key symbols |
| 92 |
^^^^^^^^^^^^^^^^^^^ |
| 93 |
|
| 94 |
The `symbol` argument is an integer that represents a "virtual" key code. |
| 95 |
It does *not* correspond to any particular numbering scheme; in particular |
| 96 |
the symbol is *not* an ASCII character code. |
| 97 |
|
| 98 |
pyglet has key symbols that are hardware and platform independent |
| 99 |
for many types of keyboard. These are defined in |
| 100 |
:py:mod:`pyglet.window.key` as constants. For example, the Latin-1 |
| 101 |
alphabet is simply the letter itself:: |
| 102 |
|
| 103 |
key.A |
| 104 |
key.B |
| 105 |
key.C |
| 106 |
... |
| 107 |
|
| 108 |
The numeric keys have an underscore to make them valid identifiers:: |
| 109 |
|
| 110 |
key._1 |
| 111 |
key._2 |
| 112 |
key._3 |
| 113 |
... |
| 114 |
|
| 115 |
Various control and directional keys are identified by name:: |
| 116 |
|
| 117 |
key.ENTER or key.RETURN |
| 118 |
key.SPACE |
| 119 |
key.BACKSPACE |
| 120 |
key.DELETE |
| 121 |
key.MINUS |
| 122 |
key.EQUAL |
| 123 |
key.BACKSLASH |
| 124 |
|
| 125 |
key.LEFT |
| 126 |
key.RIGHT |
| 127 |
key.UP |
| 128 |
key.DOWN |
| 129 |
key.HOME |
| 130 |
key.END |
| 131 |
key.PAGEUP |
| 132 |
key.PAGEDOWN |
| 133 |
|
| 134 |
key.F1 |
| 135 |
key.F2 |
| 136 |
... |
| 137 |
|
| 138 |
Keys on the number pad have separate symbols:: |
| 139 |
|
| 140 |
key.NUM_1 |
| 141 |
key.NUM_2 |
| 142 |
... |
| 143 |
key.NUM_EQUAL |
| 144 |
key.NUM_DIVIDE |
| 145 |
key.NUM_MULTIPLY |
| 146 |
key.NUM_SUBTRACT |
| 147 |
key.NUM_ADD |
| 148 |
key.NUM_DECIMAL |
| 149 |
key.NUM_ENTER |
| 150 |
|
| 151 |
Some modifier keys have separate symbols for their left and right sides |
| 152 |
(however they cannot all be distinguished on all platforms, including Mac OSX):: |
| 153 |
|
| 154 |
key.LCTRL |
| 155 |
key.RCTRL |
| 156 |
key.LSHIFT |
| 157 |
key.RSHIFT |
| 158 |
... |
| 159 |
|
| 160 |
Key symbols are independent of any modifiers being active. For example, |
| 161 |
lower-case and upper-case letters both generate the `A` symbol. This is also |
| 162 |
true of the number keypad. |
| 163 |
|
| 164 |
Modifiers |
| 165 |
^^^^^^^^^ |
| 166 |
|
| 167 |
The modifiers that are active when the event is generated are combined in a |
| 168 |
bitwise fashion and provided in the ``modifiers`` parameter. The modifier |
| 169 |
constants defined in :py:mod:`pyglet.window.key` are:: |
| 170 |
|
| 171 |
MOD_SHIFT |
| 172 |
MOD_CTRL |
| 173 |
MOD_ALT Not available on Mac OS X |
| 174 |
MOD_WINDOWS Available on Windows only |
| 175 |
MOD_COMMAND Available on Mac OS X only |
| 176 |
MOD_OPTION Available on Mac OS X only |
| 177 |
MOD_CAPSLOCK |
| 178 |
MOD_NUMLOCK |
| 179 |
MOD_SCROLLLOCK |
| 180 |
MOD_ACCEL Equivalent to MOD_CTRL, or MOD_COMMAND on Mac OS X. |
| 181 |
|
| 182 |
For example, to test if the shift key is held down:: |
| 183 |
|
| 184 |
if modifiers & MOD_SHIFT: |
| 185 |
pass |
| 186 |
|
| 187 |
Unlike the corresponding key symbols, it is not possible to determine whether |
| 188 |
the left or right modifier is held down (though you could emulate this |
| 189 |
behaviour by keeping track of the key states yourself). |
| 190 |
|
| 191 |
User-defined key symbols |
| 192 |
^^^^^^^^^^^^^^^^^^^^^^^^ |
| 193 |
|
| 194 |
pyglet does not define key symbols for every keyboard ever made. For example, |
| 195 |
non-Latin languages will have many keys not recognised by pyglet (however, |
| 196 |
their Unicode representations will still be valid, see |
| 197 |
:ref:`guide_text-and-motion-events`). |
| 198 |
Even English keyboards often have additional so-called "OEM" keys |
| 199 |
added by the manufacturer, which might be labelled "Media", "Volume" or |
| 200 |
"Shopping", for example. |
| 201 |
|
| 202 |
In these cases pyglet will create a key symbol at runtime based on the |
| 203 |
hardware scancode of the key. This is guaranteed to be unique for that model |
| 204 |
of keyboard, but may not be consistent across other keyboards with the same |
| 205 |
labelled key. |
| 206 |
|
| 207 |
The best way to use these keys is to record what the user presses after a |
| 208 |
prompt, and then check for that same key symbol. Many commercial games have |
| 209 |
similar functionality in allowing players to set up their own key bindings. |
| 210 |
|
| 211 |
Remembering key state |
| 212 |
^^^^^^^^^^^^^^^^^^^^^ |
| 213 |
|
| 214 |
:py:class:`~pyglet.window.key.KeyStateHandler` is a convenience class which |
| 215 |
stores the current keyboard state. Instances can be pushed onto the event |
| 216 |
handler stack of any window and subsequently queried using key code constants |
| 217 |
as keys:: |
| 218 |
|
| 219 |
from pyglet.window import key |
| 220 |
|
| 221 |
window = pyglet.window.Window() |
| 222 |
keys = key.KeyStateHandler() |
| 223 |
window.push_handlers(keys) |
| 224 |
|
| 225 |
# Check if the spacebar is currently pressed: |
| 226 |
if keys[key.SPACE]: |
| 227 |
pass |
| 228 |
|
| 229 |
.. _guide_text-and-motion-events: |
| 230 |
|
| 231 |
Text Input and Motion Events |
| 232 |
---------------------------- |
| 233 |
|
| 234 |
pyglet offers Unicode text input events in addition to individual key events. |
| 235 |
There are several benefits to this: |
| 236 |
|
| 237 |
* Automatic and correct mapping of platform-specific modifiers and key symbols |
| 238 |
to Unicode characters |
| 239 |
* Key repeat for held keys is automatically applied to text input according to |
| 240 |
the user's operating system preferences. |
| 241 |
* Dead keys and compose keys are automatically interpreted to produce |
| 242 |
diacritic marks or combining characters. |
| 243 |
* Keyboard input can be routed via an input palette, for example to input |
| 244 |
characters from Asian languages. |
| 245 |
* Text input can come from other user-defined sources, such as handwriting or |
| 246 |
voice recognition. |
| 247 |
|
| 248 |
The actual source of input (i.e., which keys were pressed, or what input |
| 249 |
method was used) should be considered outside of the scope of the application |
| 250 |
-- the operating system provides the necessary services. |
| 251 |
|
| 252 |
When text is entered into a window, the |
| 253 |
:py:meth:`~pyglet.window.Window.on_text` event is fired:: |
| 254 |
|
| 255 |
def on_text(text): |
| 256 |
pass |
| 257 |
|
| 258 |
The only parameter provided is a Unicode string. |
| 259 |
Although this will usually be one character long for direct keyboard |
| 260 |
input, more complex input methods such as an input palettes may provide |
| 261 |
entire words or phrases at once. |
| 262 |
|
| 263 |
How does this differ from :py:meth:`~pyglet.window.Window.on_key_press`? |
| 264 |
|
| 265 |
* Always use the :py:meth:`~pyglet.window.Window.on_text` |
| 266 |
event when you need a string from a series of keystrokes |
| 267 |
* Never use the :py:meth:`~pyglet.window.Window.on_text` event when you |
| 268 |
need individual presses, such as controlling player movement in a game |
| 269 |
|
| 270 |
|
| 271 |
.. _guide_keyboard-motion-events: |
| 272 |
|
| 273 |
Motion events |
| 274 |
^^^^^^^^^^^^^ |
| 275 |
|
| 276 |
In addition to key presses and entering new text, pyglet also supports common |
| 277 |
text editing motions: |
| 278 |
|
| 279 |
* Selecting text |
| 280 |
* Moving the caret in response to non-character keys |
| 281 |
* Deleting, copying, and pasting text |
| 282 |
|
| 283 |
pyglet automatically detects and translates platform-specific versions of |
| 284 |
supported motions into cross-platform |
| 285 |
:py:meth:`~pyglet.window.Window.on_text_motion` events. These events are |
| 286 |
intended be handled by the :py:meth:`Caret <pyglet.text.caret.Caret.on_text_motion>` |
| 287 |
of any active :py:class:`~pyglet.text.layout.IncrementalTextLayout`, such |
| 288 |
as those used in :py:class:`~pyglet.gui.widgets.TextEntry` fields. |
| 289 |
|
| 290 |
The `motion` argument to the event handler will be a constant value |
| 291 |
defined in :py:mod:`pyglet.window.key`. The table below lists the |
| 292 |
supported text motions with their keyboard mapping on each supported |
| 293 |
platform. |
| 294 |
|
| 295 |
.. list-table:: |
| 296 |
:header-rows: 1 |
| 297 |
|
| 298 |
* - Constant |
| 299 |
- Behaviour |
| 300 |
- Windows/Linux |
| 301 |
- Mac OS X |
| 302 |
* - ``MOTION_UP`` |
| 303 |
- Move the cursor up |
| 304 |
- Up |
| 305 |
- Up |
| 306 |
* - ``MOTION_DOWN`` |
| 307 |
- Move the cursor down |
| 308 |
- Down |
| 309 |
- Down |
| 310 |
* - ``MOTION_LEFT`` |
| 311 |
- Move the cursor left |
| 312 |
- Left |
| 313 |
- Left |
| 314 |
* - ``MOTION_RIGHT`` |
| 315 |
- Move the cursor right |
| 316 |
- Right |
| 317 |
- Right |
| 318 |
* - ``MOTION_COPY`` |
| 319 |
- Copy the current selection to the clipboard |
| 320 |
- Ctrl + C |
| 321 |
- Command + C |
| 322 |
* - ``MOTION_PASTE`` |
| 323 |
- Paste the clipboard contents into the current document |
| 324 |
- Ctrl + V |
| 325 |
- Command + V |
| 326 |
* - ``MOTION_PREVIOUS_WORD`` |
| 327 |
- Move the cursor to the previous word |
| 328 |
- Ctrl + Left |
| 329 |
- Option + Left |
| 330 |
* - ``MOTION_NEXT_WORD`` |
| 331 |
- Move the cursor to the next word |
| 332 |
- Ctrl + Right |
| 333 |
- Option + Right |
| 334 |
* - ``MOTION_BEGINNING_OF_LINE`` |
| 335 |
- Move the cursor to the beginning of the current line |
| 336 |
- Home |
| 337 |
- Command + Left |
| 338 |
* - ``MOTION_END_OF_LINE`` |
| 339 |
- Move the cursor to the end of the current line |
| 340 |
- End |
| 341 |
- Command + Right |
| 342 |
* - ``MOTION_PREVIOUS_PAGE`` |
| 343 |
- Move to the previous page |
| 344 |
- Page Up |
| 345 |
- Page Up |
| 346 |
* - ``MOTION_NEXT_PAGE`` |
| 347 |
- Move to the next page |
| 348 |
- Page Down |
| 349 |
- Page Down |
| 350 |
* - ``MOTION_BEGINNING_OF_FILE`` |
| 351 |
- Move to the beginning of the document |
| 352 |
- Ctrl + Home |
| 353 |
- Home |
| 354 |
* - ``MOTION_END_OF_FILE`` |
| 355 |
- Move to the end of the document |
| 356 |
- Ctrl + End |
| 357 |
- End |
| 358 |
* - ``MOTION_BACKSPACE`` |
| 359 |
- Delete the previous character |
| 360 |
- Backspace |
| 361 |
- Backspace |
| 362 |
* - ``MOTION_DELETE`` |
| 363 |
- Delete the next character, or the current character |
| 364 |
- Delete |
| 365 |
- Delete |
| 366 |
|
| 367 |
If you believe pyglet needs to add support for a motion which is |
| 368 |
currently missing, please skip to |
| 369 |
:ref:`guide_keyboard-adding-new-motion-events`. |
| 370 |
|
| 371 |
Customizing this behavior for an individual project is currently |
| 372 |
difficult due to the way carets and text entry fields are interconnected. |
| 373 |
However, using :py:meth:`~pyglet.window.Window.on_key_press` to handle |
| 374 |
motion events should still be avoided for the following reasons: |
| 375 |
|
| 376 |
* Supported platforms can assign a key to different motions. For example |
| 377 |
the Home key moves the cursor to the start of a line on Windows, but |
| 378 |
to the beginning of a document on Mac OS. |
| 379 |
* Users expect holding down a motion's keys to repeat it released. For |
| 380 |
example, holding Backspace deletes multiple characters. However, only |
| 381 |
one :py:meth:`~pyglet.window.Window.on_key_press` event occurs per |
| 382 |
keypress. |
| 383 |
|
| 384 |
|
| 385 |
.. _guide_keyboard-adding-new-motion-events: |
| 386 |
|
| 387 |
Adding New Motions |
| 388 |
"""""""""""""""""" |
| 389 |
|
| 390 |
Before adding a new motion, please do the following: |
| 391 |
|
| 392 |
#. Consult the previous section & each platform's documentation to be |
| 393 |
sure it is: |
| 394 |
|
| 395 |
#. A common text operation present on every platform |
| 396 |
#. Not already implemented by pyglet |
| 397 |
|
| 398 |
#. Attempt to find the corresponding functionality in |
| 399 |
`Apple's NSTextView documentation |
| 400 |
<https://developer.apple.com/documentation/appkit/nstextview/>`_ |
| 401 |
|
| 402 |
#. Discuss the addition and any remaining questions with maintainers by either: |
| 403 |
|
| 404 |
* `Filing a GitHub Issue <https://github.com/pyglet/pyglet/issues>`_ |
| 405 |
* `Joining the Discord server <https://github.com/pyglet/pyglet#contact>`_ |
| 406 |
|
| 407 |
Then, once you're ready: |
| 408 |
|
| 409 |
#. Add the motion constant to :py:mod:`pyglet.window.key` |
| 410 |
|
| 411 |
#. Add an entry for the constant in the :ref:`guide_keyboard-motion-events` |
| 412 |
section |
| 413 |
|
| 414 |
#. Implement shared handling behavior in :py:meth:`~pyglet.text.caret.Caret.on_text_motion` |
| 415 |
|
| 416 |
#. Implement Mac support (usually the most confusing step) |
| 417 |
|
| 418 |
#. Open `pyglet/window/cocoa/pyglet_textview.py |
| 419 |
<https://github.com/pyglet/pyglet/blob/master/pyglet/window/cocoa/pyglet_textview.py>`_ |
| 420 |
#. Implement a corresponding handler method on |
| 421 |
``PygletTextView_Implementation`` (pyglet's subclass of ``NSTextView``) |
| 422 |
|
| 423 |
#. Add the Windows keyboard shortcut |
| 424 |
|
| 425 |
#. Open `pyglet/window/win32/__init__.py |
| 426 |
<https://github.com/pyglet/pyglet/blob/master/pyglet/window/win32/__init__.py>`_ |
| 427 |
#. Add the keyboard shortcut to the ``_motion_map`` dictionary |
| 428 |
|
| 429 |
#. Add the Linux keyboard shortcut |
| 430 |
|
| 431 |
#. Open `pyglet/window/xlib/__init__.py |
| 432 |
<https://github.com/pyglet/pyglet/blob/master/pyglet/window/xlib/__init__.py>`_ |
| 433 |
#. Add the keyboard shortcut to the ``_motion_map`` dictionary |
| 434 |
|
| 435 |
|
| 436 |
Be sure to test your changes before making a PR if possible! |
| 437 |
|
| 438 |
If you do not have access to a specific platform above, include this in your PR's |
| 439 |
notes. |
| 440 |
|
| 441 |
Keyboard exclusivity |
| 442 |
-------------------- |
| 443 |
|
| 444 |
Some keystrokes or key combinations normally bypass applications and are |
| 445 |
handled by the operating system. Some examples are Alt+Tab (Command+Tab on |
| 446 |
Mac OS X) to switch applications and the keys mapped to Expose on Mac OS X. |
| 447 |
|
| 448 |
You can disable these hot keys and have them behave as ordinary keystrokes for |
| 449 |
your application. This can be useful if you are developing a kiosk |
| 450 |
application which should not be closed, or a game in which it is possible for |
| 451 |
a user to accidentally press one of these keys. |
| 452 |
|
| 453 |
To enable this mode, call |
| 454 |
:py:meth:`Window.set_exclusive_keyboard <pyglet.window.Window.set_exclusive_keyboard>` |
| 455 |
on the window it should apply to. On Mac OS X, the dock and menu bar |
| 456 |
will slide out of view while exclusive keyboard is activated. |
| 457 |
|
| 458 |
The following restrictions apply on Windows: |
| 459 |
|
| 460 |
* Only Alt+Tab can be disabled |
| 461 |
* Users will still be able to switch applications through: |
| 462 |
|
| 463 |
* Ctrl+Escape |
| 464 |
* Alt+Escape |
| 465 |
* the Windows key |
| 466 |
* Ctrl+Alt+Delete |
| 467 |
|
| 468 |
The following restrictions apply on Mac OS X: |
| 469 |
|
| 470 |
* The power key is not disabled. |
| 471 |
|
| 472 |
Use of this function is not recommended for general release applications or |
| 473 |
games as it violates user-interface conventions. |