In this Python Pyglet article i want to show How to Handle Keyboard Press Events in Python Pyglet.so first of all let’s talk about Python Pyglet and also we want to talk about installation of Python Pyglet.
What is Python Pyglet?
Pyglet is a cross platform multimedia library for Python. It provides an easy interface for creating games, interactive applications, multimedia software and many more. Pyglet is built on top of OpenGL and provides bindings for audio, video and input devices, and this is good for different types of multimedia applications.
How to Install Pyglet?
You can install Python Pyglet using pip like this.
1 |
pip install pyglet |
How to Handle Keyboard Events in Python Pyglet?
Pyglet has support for low-level keyboard input suitable for games as well as locale and device-independent Unicode text entry.
Keyboard input requires a window which has focus. The operating system usually decides which application window has keyboard focus. Typically this window appears above all others and may be decorated differently, though this is platform-specific (for example, Unix window managers sometimes couple keyboard focus with the mouse pointer).
You can request keyboard focus for a window with the activate() method, but you should not rely on this – it may simply provide a visual cue to the user indicating that the window requires user input, without actually getting focus.
Windows created with the WINDOW_STYLE_BORDERLESS or WINDOW_STYLE_TOOL style cannot receive keyboard focus.
It is not possible to use pyglet’s keyboard or text events without a window; consider using Python built-in functions such as input instead.
There are two Keyboard Events that you can use in Python Pyglet
1 2 3 4 |
def on_key_press(symbol, modifiers): pass def on_key_release(symbol, modifiers): pass |
And also there are different and directional keys in Pyglet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
key.ENTER or key.RETURN key.SPACE key.BACKSPACE key.DELETE key.MINUS key.EQUAL key.BACKSLASH key.LEFT key.RIGHT key.UP key.DOWN key.HOME key.END key.PAGEUP key.PAGEDOWN key.F1 key.F2 |
This is the complete code for this article
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 |
import pyglet from pyglet.window import key # Create Pyglet window window = pyglet.window.Window() # Define event handler for key press events @window.event def on_key_press(symbol, modifiers): # Check which key was pressed using its symbol if symbol == key.A: print("A Key Was Pressed") elif symbol == key.B: print("B Key Was Pressed") elif symbol == key.ENTER: print("Enter Key Was Pressed") # Define event handler for window drawing @window.event def on_draw(): # Clear window window.clear() # Start Pyglet event loop pyglet.app.run() |
So these line of codes are for our key press, so if a user press A key i want to show that A key was pressed and the same action for B and Enter Keys.
1 2 3 4 5 6 7 8 9 10 |
@window.event def on_key_press(symbol, modifiers): if symbol == key.A: print("A Key Was Pressed") elif symbol == key.B: print("B Key Was Pressed") elif symbol == key.ENTER: print("Enter Key Was Pressed") |
You can run the code and press on the window, you will see the result as expected
Subscribe and Get Free Video Courses & Articles in your Email