Table of contents
1.
Introduction
2.
Pyglet.input
3.
Classes
4.
Functions
5.
Exceptions
6.
Frequently Asked Questions
6.1.
How many exception classes are present in pyglet.input?
6.2.
What is a pyglet?
6.3.
What does pyglet.input do?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

Pyglet.input

Author Aditi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we will learn about the Pyglet.input module in detail. We are going to learn about all the functions provided in this module. This pyglet module is designed purely in python language. Because it was intended in Python, you must have prerequisites for programming in the python language.

Let's dive into the article to get more information about pyglet.input.

Pyglet.input

Apart from Windows's standard mouse and keyboard functionality, the pyglet.input module provides a single interface to nearly any input device. Get_devices() returns a list of all supported devices, including joysticks, tablets, space controllers, wheels, pedals, remote controls, keyboards, and mouse, at the most basic level. Depending on the operating system (and, of course, what's plugged in), the list of returning devices changes significantly. At this level, pyglet doesn't try to figure out what a device is, just what controls it offers. 

A control can be a button with a True or False value or a relative or absolute-valued axis with a float value. Occasionally, the name of a command is supplied (for example, x, which represents the horizontal axis of a joystick), although this is not always the case.

To use a device for input while using this module, we will need the following functions:

  • To get and identify the device, use get_devices(), get_apple_remote(), or get_joysticks().
  • Query the devices list of controls for low-level devices (retrieved with get_devices()) to see which ones you're interested in. The interface provides a set of controls for high-level interfaces.
  • Attach event handlers to device controllers if desired.
  • To start receiving events on the device, call device.open(). After this time, you can begin querying the control values; they will be updated asynchronously.
  • When you're done with the device, use the device.close() (not needed if your application quits at this time).

Classes

class device(display, name)

Bases: object

Input device.

Variables:-

  • display (pyglet.canvas.Display) — The display this device is linked to.
  • name (str) – The device's name, as defined by the firmware.
  • manufacturer (str) – The device's manufacturer, or none if the information isn't accessible.

 

class Control(name, raw_name=None)

Bases: pyglet.event.EventDispatcher

A device provides single value input. When the device is open, the control value can be accessed. When the value changes, event handlers may be linked to the control and invoked. The device's min and max attributes are presented; the control's value may be outside this range in some situations.

Variables:

  • name (str) – The control's name, or None if unknown 
  • raw_name (str) – The control's unmodified name, as supplied by the operating system; or None if unknown
  • inverted (bool) - If True, the value reported is reversed from what the device reported; this is typically done to provide uniformity between operating systems.

 

class RelativeAxis(name, raw_name=None)

Bases: pyglet.input.base.Control

 

class AbsoluteAxis(name, min, max, raw_name=None)

Bases: pyglet.input.base.Control

The value of this axis reflects a change in the initial value and indicates a physical measurement taken by the instrument. The advertised range is between min and max.

Variables: 

  • min (float) — Advertised minimum value.
  • max (float) – Maximum value advertised.

 

class Button(name, raw_name=None)

Bases: pyglet.input.base.Control

A control with a boolean value.
 

class Joystick(device)

Bases: pyglet.event.EventDispatcher

Joystick-like devices have a high-level interface. Joysticks, gamepads, gaming controllers, and maybe even steering wheels and other input devices fall within this category. Unfortunately, there is no method to discriminate between these various device kinds.

To utilize a joystick, first call open, then inspect the values of x, y, and so on in your game loop. These numbers have been scaled to [-1.0, 1.0].

Attach a joy axis motion event handler to the joystick to receive events when the value of an axis changes. This event gets the Joystick instance, axis name, and current value as arguments. 

Variables: 

  • device (Device) — This joystick interface's underlying device.
  • (float) – The current X (horizontal) value, which ranges from -1.0 (left) to 1.0 (right) (right).
  • (float) – The current y (vertical) value, which ranges from -1.0 (top) to 1.0 (bottom) (bottom).
  • (float) — The current Z value can be anywhere between -1.0 and 1.0. The throttle control is commonly the Z value on joysticks. The Z value is commonly the secondary thumb vertical axis on gaming controllers.
  • rx (float) – The current rotational X value can be anywhere between -1.0 and 1.0.
  • ry (float) — The current rotational Y value can be anywhere between -1.0 and 1.0.
  • rz (float) — The current rotational Z value can be anywhere between -1.0 and 1.0. The RZ number on joysticks is generally the twist of the stick. The RZ value is commonly the secondary thumb horizontal axis on gaming controllers.
  • hat_x (int) – Current horizontal hat (POV) position; one of -1 (left), 0 (centre), or 1 (right) (right).
  • hat_y (int) – Vertical position of the current hat (POV); one of -1 (bottom), 0 (centre), or 1 (top) (top).
  • buttons (bool) — A list of boolean values that indicate the current state of the buttons. These are arranged in ascending order. Thus button 1 has a value at buttons[0], and so on.
  • x_control (AbsoluteAxis) – The underlying control for the x value, or None if none exists.
  • y_control (AbsoluteAxis) – The underlying control for the y value, or None if none exists.
  • z_control (AbsoluteAxis) – Z value's underlying control, or None if none is supplied.
  • rx_control (AbsoluteAxis) – Rx value's underlying control, or None if none is provided.
  • ry_control (AbsoluteAxis) – The underlying control for the ry value, or None if none exists.
  • rz_control (AbsoluteAxis) – The underlying control for the rz value, or None if none exists.
  • hat_x_control (AbsoluteAxis) – The underlying control for the hat x value, or None if none exists.
  • hat_y_control (AbsoluteAxis) – The underlying control for the hat y value, or None if none exists.
  • button_controls (list of the button) – Button values' underlying controls.
     
class AppleRemote(device)

Bases: pyglet.event.EventDispatcher

It provides Apple remote control interface at a high level. This interface gives you access to the remote's six buttons. Certain buttons on the remote are treated as a distinct control when pressed and held.

Variables:

  • This interface's underlying device is called device (Device).
  • left_control (Button) – Button control for the left (prev) button.
  • left_hold_control (Button) – Button control for holding the left button (rewind).
  • right_control (Button) — Right (next) button button control.
  • right_hold_control (Button) – Controls the right button when it is pressed (fast forward).
  • up_control (Button) — The control of the up (volume increase) button.
  • down_control (Button) - Down (volume drop) button button control.
  • select_control (Button) — Select (play/pause) button button control.
  • select_hold_control (Button) – Button control for holding the select button.
  • menu_control (Button) — The menu button's button control.
  • menu_hold_control (Button) – Hold the menu button using this button control.

 

class Tablet

It provides a tablet device interface at a high level. Tablets, unlike other gadgets, must be opened for a particular window and cannot be used only for that purpose. The open function returns a TabletCanvas object that supports all the tablet's events.

Currently, only one tablet device may be utilized; however, many windows can be opened. The behavior is unknown when more than one tablet is connected.

Functions

get_apple_remote(display=None)

Get yourself an Apple remote control.

The Apple remote is a compact white 6-button remote control that comes standard with most new Apple computers and laptops. Only Mac OS X may be used with the remote.
 

get_devices(display=None)

Get a list of all input devices that are connected.

 

get_joysticks(display=None)

Get a list of joysticks that are connected.

 

get_tablets(display=None)

Obtain a list of tablet computers.

Even if no tablet device is attached, this function may return a simple tablet device (for example, it is not possible on Mac OS X to determine if a tablet device is connected). Even though pyglet returns a list of tablets, it does not yet support multiple tablets, and if more than one is attached, the behavior is unknown.

Exceptions

  • class DeviceException
  • class DeviceOpenException
  • class DeviceExclusiveException

Frequently Asked Questions

How many exception classes are present in pyglet.input?

There are three exceptions present in pyglet.input, and they are class DeviceException, class DeviceOpenException, and class DeviceExclusiveException

What is a pyglet?

Pyglet is a simple yet powerful framework for creating aesthetically rich GUI programs for Windows, Mac OS, and Linux, such as games and multimedia.

What does pyglet.input do?

Pyglet.input module provides a single interface to nearly any input device apart from Windows's standard mouse and keyboard functionality.

Conclusion

In this article, we have extensively discussed the pyglet.input module of python programming language.

We hope this blog has helped you enhance your knowledge regarding the pyglet.input module of python. Some official documentation on pyglet python programming language that can help you improve your understanding is Pygamepygletpyglet documentationaugmented reality, and blender.

Check out this problem - Smallest Distinct Window.

If you would like to learn more, check out our articles on pygletpython libraries, and the basics of python

Practice makes a man perfect. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass