Introduction
Have you ever wondered why these gaming apps are so powerful and easy to use, why the games becoming more advanced technology, and what technology they used?
All of these questions have only one answer, which is pyglet. Pyglet is a technology that works mainly on gaming using python technology.
So today, we will learn more about pyglet and how it works with the keyboard.
Working with Keyboard
Pyglet supports low-level keyboard input for games and locale- and device-independent Unicode text entry.
For keyboard input, you'll need a window with focus. In most circumstances, the operating system determines which programme window has keyboard focus. This window is usually displayed above all others and can be styled in various ways. This, however, differs by platform.
A window's keyboard focus can be requested using the activate() method. Even so, it's best not to rely on this because it could just be a visual indication to the user, indicating that the window requires input without concentrating.
Keyboard Events
When any key on the keyboard is hit or released, the on_key_ press() and on_key_release() events are triggered. Key repeat does not affect these events; once a key is pushed, it has no more events until it is released.
Both events are parameterized using the same arguments:
def on_key_press(symbols, modifiers):
print(“The key is pressed”)
def on_key_release(symbols, modifiers):
print(“ The key is released”)
Symbols
The symbol argument, an integer, represents a virtual key code. The sign is not an ASCII character code and does not correspond to any numbering scheme.
For many different types of keyboards, pyglet includes critical indicators that are hardware and platform-independent. These are defined as constants in pyglet.window.key.
For example:
- key.N
- key._5
- key.F5
- key.DELETE
Modifiers
When the event is formed, the modifiers held down are bitwise concatenated and delivered in the modifiers argument. The modifier constants in the pyglet.window.key is as follows:
- MOD_SHIFT
- MOD_CTRL
- MOD_NUMLOCK
- MOD_SCROLLLOCK
Key State
Pyglet includes the KeyStateHandler convenience class for storing and remembering the current keyboard state. This may be pushed onto any window's event handler stack and then queried:
from pyglet.window import key
# Creating the window
window = pyglet.window.Window(width, height, title)
keys = key.KeyStateHandler()
window.push_handlers(keys)
# Check if the backslash is currently pressed:
if keys[key.BACKSLASH]:
pass