This module contains functions for dealing with the keyboard.
The pygame.eventpygame module for interacting with events and queues queue gets pygame.KEYDOWN and pygame.KEYUP events when the keyboard buttons are pressed and released. Both events have key and mod attributes.
- key: an integer ID representing every key on the keyboard
- mod: a bitmask of all the modifier keys that were in a pressed state when the event occurred
The pygame.KEYDOWN event has the additional attributes unicode and scancode.
- unicode: a single character string that is the fully translated character entered, this takes into account the shift and composition keys
- scancode: the platform-specific key code, which could be different from keyboard to keyboard, but is useful for key selection of weird keys like the multimedia keys
New in pygame 2.0.0: The pygame.TEXTINPUT event is preferred to the unicode attribute of pygame.KEYDOWN. The attribute text contains the input.
The following is a list of all the constants [from pygame.localspygame constants] used to represent keyboard keys.
Portability note: The integers for key constants differ between pygame 1 and 2. Always use key constants [K_a] rather than integers directly [97] so that your key handling code works well on both pygame 1 and pygame 2.
The keyboard also has a list of modifier states [from pygame.localspygame constants] that can be assembled by bitwise-ORing them together.
The modifier information is contained in the mod attribute of the pygame.KEYDOWN and pygame.KEYUP events. The mod attribute is a bitmask of all the modifier keys that were in a pressed state when the event occurred. The modifier information can be decoded using a bitwise AND [except for KMOD_NONE, which should be compared using equals ==]. For example:
Returns True when the display window has keyboard focus from the system. If the display needs to ensure it does not lose keyboard focus, it can use pygame.event.set_grab[]control the sharing of input devices with other applications to grab all input.
pygame.key.get_pressed[]¶Returns a sequence of boolean values representing the state of every key on the keyboard. Use the key constant values to index the array. A True value means the that button is pressed.
Note
Getting the list of pushed buttons with this function is not the proper way to handle text entry from the user. There is no way to know the order of keys pressed, and rapidly pushed keys can be completely unnoticed between two calls to pygame.key.get_pressed[]. There is also no way to translate these pushed keys into a fully translated character value. See the pygame.KEYDOWN events on the pygame.eventpygame module for interacting with events and queues queue for this functionality.
Returns a single integer representing a bitmask of all the modifier keys being held. Using bitwise operators you can test if specific modifier keys are pressed.
pygame.key.set_mods[]¶Create a bitmask of the modifier key constants you want to impose on your program.
pygame.key.set_repeat[]¶When the keyboard repeat is enabled, keys that are held down will generate multiple pygame.KEYDOWN events. The delay parameter is the number of milliseconds before the first repeated pygame.KEYDOWN event will be sent. After that, another pygame.KEYDOWN event will be sent every interval milliseconds. If a delay value is provided and an interval value is not provided or is 0, then the interval will be set to the same value as delay.
To disable key repeat call this function with no arguments or with delay set to 0.
When pygame is initialized the key repeat is disabled.
Raises:ValueError -- if delay or interval is < 0Changed in pygame 2.0.0: A ValueError is now raised [instead of a pygame.error] if delay or interval is < 0.
Get the delay and interval keyboard repeat values. Refer to pygame.key.set_repeat[]control how held keys are repeated for a description of these values.
New in pygame 1.8.
Get the descriptive name of the button from a keyboard button id constant.
pygame.key.key_code[]¶Get the key identifier code from the descriptive name of the key. This returns an integer matching one of the K_* keycodes. For example:
- ValueError -- if the key name is not known.
- NotImplementedError -- if used with SDL 1.
New in pygame 2.0.0.
Start receiving pygame.TEXTEDITING and pygame.TEXTINPUT events.
A pygame.TEXTEDITING event is received when an IME composition is started or changed. It contains the composition text, length, and editing start position within the composition [attributes text, length, and start, respectively]. When the composition is committed [or non-IME input is received], a pygame.TEXTINPUT event is generated.
Text input events handling is on by default.
New in pygame 2.0.0.
Stop receiving pygame.TEXTEDITING and pygame.TEXTINPUT events.
Text input events handling is on by default
New in pygame 2.0.0.
This sets the rectangle used for typing with an IME. It controls where the candidate list will open, if supported.
New in pygame 2.0.0.
Edit on GitHub