Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Working with Keyboard
2.1.
Keyboard Events
2.1.1.
Symbols
2.1.2.
Modifiers
2.2.
Key State
3.
Text and Motion Events
3.1.
Motion Events
4.
Frequently Asked Questions
4.1.
What exactly is a pyglet?
4.2.
Is it possible for anyone to use Pyglet?
4.3.
Which one performs better: Pyglet or Pygame?
5.
Conclusion
Last Updated: Mar 27, 2024

Working with the Keyboard in Pyglet

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Text and Motion Events

Pyglet separates the user's input Unicode text from the keys they press. This has several advantages:

  • The difficult task of mapping modifiers and key symbols to Unicode characters is handled correctly and automatically.
  • According to the user's operating system options, key repeat is applied to keys held down.
  • Compose, and dead keys are automatically interpreted to generate diacritic marks or combine characters.
  • An input palette can redirect keyboard input to characters from Asian languages.
     

The actual input source (which keys were hit or which input method was utilised) should be considered outside the application's scope — the operating system provides the essential functions.

The on text() event is triggered when text is inserted into a window:

def on_text(text):
    pass

 

Only a Unicode string is provided as an argument. This will typically be one character long for keyboard input. More complex input techniques, such as an input palette, may, on the other hand, provide a full word or phrase at once.

Motion Events

Users navigate text widgets by pressing keys on the keyboard, following well-established standards, and entering text. The left arrow key, for example, advances the pointer one character to the left.

 

While it may be tempting to record these events using the on_key_press() event, there are a few drawbacks:

  • Although on_key_press() does not emit key repeat events, users believe that holding down the left arrow key will eventually advance the character to the start of the line.
     
  • Different operating systems have other conventions for how keys behave. For example, on Windows, the Home key is used to move the pointer to the beginning of the line, whereas on Mac OS X, the same key brings the cursor to an end.
     

The on_text_motion() event in Pyglet windows solves these issues by abstracting away key presses and presenting your application with only the required cursor motion:

def on_text_motion(motion):
    pass

 

Motion is an integer that is declared in pyglet_window_key as a constant.

Frequently Asked Questions

What exactly is a pyglet?

On Windows, Mac OS X, and Linux, pyglet is a sophisticated yet straightforward Python library for creating games and other graphically rich applications. Windowing, user interface event management, joysticks, OpenGL graphics, loading photos, movies, and sound and music playback are all supported. This comes with a user-friendly Pythonic API that's easy to pick up and doesn't get in the way.

Is it possible for anyone to use Pyglet?

Pyglet is released under the BSD open-source license, allowing it to use in commercial and open-source projects with few restrictions.

Which one performs better: Pyglet or Pygame?

Pyglet is faster and performs better than pygame; nowadays, speed is always a factor in designing a game.

Conclusion

This article extensively discusses the working of pyglet with the keyboard and different types of keyboard events and defines vital symbols.

We hope that this blog has helped you enhance your knowledge regarding the working of keyboards in Pyglet, and if you would like to grasp more, check out our articles on Pyglet and Pygame.  

Check out our article on 19 Best Python Frameworks and learn more about Python by visiting the Python category at Coding Ninjas. Visit our excellent articles to understand basic concepts of Python like Arrays, Lists, Iterators, generatorsVariablesFunctions, etc.

Do upvote our blog to help other ninjas grow.

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations, and much more!!

We wish you Good Luck! Keep coding and keep reading Ninja!!

Previous article
Windowing in Pyglet
Next article
Playing with the Mouse in Pyglet
Live masterclass