pyglet provides multiple types of keyboard input abstraction:
Cross-platform key press/release events suitable for game controls
Unicode text entry with automatic locale and platform handling
Cross-platform detection of common text editing actions
All of them have the following restrictions:
There must be at least one pyglet
Windowinstance which can hold keyboard focus
Windows created with the following styles cannot hold keyboard focus:
If your project’s requirements fall outside these restrictions, you should consider alternatives. Examples include:
Keyboard Focus Conventions
Keyboard focus is where the user’s keyboard input is sent.
Desktop operating systems often follow these conventions:
Only one window can have focus
Clicking a window gives it focus
The window with focus is displayed above all others
The window with focus has a distinct title bar style
Windows can have focus taken away
Windows can request focus
However, the items above are not guaranteed to be true.
For example, pyglet allows you to request focus from the OS by calling
the OS may not support the feature. Even if it does support it, the OS
may not only refuse, but do so without notifying the user focus was
Deviations from the conventions can occur for any of the following reasons:
Permission requests and error notifications
Window focus is set to follow the mouse
Split screen utilities and Linux window managers with multi-focus modes
on_key_release() events are fired when
any key on the keyboard is pressed or released, respectively. These events
are not affected by “key repeat” – once a key is pressed there are no more
events for that key until it is released.
Both events are parameterised by the same arguments:
def on_key_press(symbol, modifiers):
def on_key_release(symbol, modifiers):
Defined key symbols
The symbol argument is an integer that represents a “virtual” key code. It does not correspond to any particular numbering scheme; in particular the symbol is not an ASCII character code.
pyglet has key symbols that are hardware and platform independent
for many types of keyboard. These are defined in
pyglet.window.key as constants. For example, the Latin-1
alphabet is simply the letter itself:
The numeric keys have an underscore to make them valid identifiers:
Various control and directional keys are identified by name:
key.ENTER or key.RETURN
Keys on the number pad have separate symbols:
Some modifier keys have separate symbols for their left and right sides (however they cannot all be distinguished on all platforms, including Mac OSX):
Key symbols are independent of any modifiers being active. For example, lower-case and upper-case letters both generate the A symbol. This is also true of the number keypad.
The modifiers that are active when the event is generated are combined in a
bitwise fashion and provided in the
modifiers parameter. The modifier
constants defined in
MOD_ALT Not available on Mac OS X
MOD_WINDOWS Available on Windows only
MOD_COMMAND Available on Mac OS X only
MOD_OPTION Available on Mac OS X only
MOD_ACCEL Equivalent to MOD_CTRL, or MOD_COMMAND on Mac OS X.
For example, to test if the shift key is held down:
if modifiers & MOD_SHIFT:
Unlike the corresponding key symbols, it is not possible to determine whether the left or right modifier is held down (though you could emulate this behaviour by keeping track of the key states yourself).
User-defined key symbols
pyglet does not define key symbols for every keyboard ever made. For example, non-Latin languages will have many keys not recognised by pyglet (however, their Unicode representations will still be valid, see Text Input and Motion Events). Even English keyboards often have additional so-called “OEM” keys added by the manufacturer, which might be labelled “Media”, “Volume” or “Shopping”, for example.
In these cases pyglet will create a key symbol at runtime based on the hardware scancode of the key. This is guaranteed to be unique for that model of keyboard, but may not be consistent across other keyboards with the same labelled key.
The best way to use these keys is to record what the user presses after a prompt, and then check for that same key symbol. Many commercial games have similar functionality in allowing players to set up their own key bindings.
Remembering key state
KeyStateHandler is a convenience class which
stores the current keyboard state. Instances can be pushed onto the event
handler stack of any window and subsequently queried using key code constants
from pyglet.window import key
window = pyglet.window.Window()
keys = key.KeyStateHandler()
# Check if the spacebar is currently pressed:
Text Input and Motion Events
pyglet offers Unicode text input events in addition to individual key events. There are several benefits to this:
Automatic and correct mapping of platform-specific modifiers and key symbols to Unicode characters
Key repeat for held keys is automatically applied to text input according to the user’s operating system preferences.
Dead keys and compose keys are automatically interpreted to produce diacritic marks or combining characters.
Keyboard input can be routed via an input palette, for example to input characters from Asian languages.
Text input can come from other user-defined sources, such as handwriting or voice recognition.
The actual source of input (i.e., which keys were pressed, or what input method was used) should be considered outside of the scope of the application – the operating system provides the necessary services.
When text is entered into a window, the
on_text() event is fired:
The only parameter provided is a Unicode string. Although this will usually be one character long for direct keyboard input, more complex input methods such as an input palettes may provide entire words or phrases at once.
How does this differ from
Always use the
on_text()event when you need a string from a series of keystrokes
Never use the
on_text()event when you need individual presses, such as controlling player movement in a game
In addition to key presses and entering new text, pyglet also supports common text editing motions:
Moving the caret in response to non-character keys
Deleting, copying, and pasting text
pyglet automatically detects and translates platform-specific versions of
supported motions into cross-platform
on_text_motion() events. These events are
intended be handled by the
of any active
as those used in
The motion argument to the event handler will be a constant value
pyglet.window.key. The table below lists the
supported text motions with their keyboard mapping on each supported
Mac OS X
Move the cursor up
Move the cursor down
Move the cursor left
Move the cursor right
Copy the current selection to the clipboard
Ctrl + C
Command + C
Paste the clipboard contents into the current document
Ctrl + V
Command + V
Move the cursor to the previous word
Ctrl + Left
Option + Left
Move the cursor to the next word
Ctrl + Right
Option + Right
Move the cursor to the beginning of the current line
Command + Left
Move the cursor to the end of the current line
Command + Right
Move to the previous page
Move to the next page
Move to the beginning of the document
Ctrl + Home
Move to the end of the document
Ctrl + End
Delete the previous character
Delete the next character, or the current character
If you believe pyglet needs to add support for a motion which is currently missing, please skip to Adding New Motions.
Customizing this behavior for an individual project is currently
difficult due to the way carets and text entry fields are interconnected.
on_key_press() to handle
motion events should still be avoided for the following reasons:
Supported platforms can assign a key to different motions. For example the Home key moves the cursor to the start of a line on Windows, but to the beginning of a document on Mac OS.
Users expect holding down a motion’s keys to repeat it released. For example, holding Backspace deletes multiple characters. However, only one
on_key_press()event occurs per keypress.
Adding New Motions
Before adding a new motion, please do the following:
Consult the previous section & each platform’s documentation to be sure it is:
A common text operation present on every platform
Not already implemented by pyglet
Attempt to find the corresponding functionality in Apple’s NSTextView documentation
Discuss the addition and any remaining questions with maintainers by either:
Then, once you’re ready:
Add the motion constant to
Add an entry for the constant in the Motion events section
Implement shared handling behavior in
Implement Mac support (usually the most confusing step)
Implement a corresponding handler method on
PygletTextView_Implementation(pyglet’s subclass of
Add the Windows keyboard shortcut
Add the keyboard shortcut to the
Add the Linux keyboard shortcut
Add the keyboard shortcut to the
Be sure to test your changes before making a PR if possible!
If you do not have access to a specific platform above, include this in your PR’s notes.
Some keystrokes or key combinations normally bypass applications and are handled by the operating system. Some examples are Alt+Tab (Command+Tab on Mac OS X) to switch applications and the keys mapped to Expose on Mac OS X.
You can disable these hot keys and have them behave as ordinary keystrokes for your application. This can be useful if you are developing a kiosk application which should not be closed, or a game in which it is possible for a user to accidentally press one of these keys.
To enable this mode, call
on the window it should apply to. On Mac OS X, the dock and menu bar
will slide out of view while exclusive keyboard is activated.
The following restrictions apply on Windows:
Only Alt+Tab can be disabled
Users will still be able to switch applications through:
the Windows key
The following restrictions apply on Mac OS X:
The power key is not disabled.
Use of this function is not recommended for general release applications or games as it violates user-interface conventions.