Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Pyglet Input
3.
Using the Joysticks
3.1.
Axis Parameters
3.2.
Status Change of Joystick
3.3.
Joystick Parameters
3.4.
Motion Events
4.
Using the Apple Remote
4.1.
Methods for Using Remote
5.
Frequently Asked Questions
5.1.
Is the joystick a controller?
5.2.
What is the history behind the name Joystick?
5.3.
In Pyglet, what is the apple remote?
6.
Conclusion
Last Updated: Mar 27, 2024

Working with Other Input devices in Pyglet

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

Introduction

You might have read about the keyboards working in pyglet and mouse working in pyglet. Now, we will learn about other input devices working with pyglet to give you an exotic gaming experience.

Pyglet can work with the joystick and apple remote as an input device. We will deep dive into the technical and programming part of how it works with these devices.

Pyglet Input

Apart from the standard mouse and keyboard functionality offered by Windows, the pyglet.input module provides a single interface to nearly any input device. The get_devices() method returns a list of all supported devices, including joysticks, tablets, apple remotes, wheels, pedals, remote controls, keyboards, and mouses, at the most basic level. 

Depending on the operating system (and the plugged-in), the list of returning devices changes significantly.

Joysticks, tablets, and the Apple remote control all have higher-level interfaces. Pyglet can typically positively identify these devices, and a single interface provides a primary degree of functionality for all of them.

 

Follow the following algorithm to use an input device in pyglet,

Step 1: First, gather and verify input devices with get_devices()get_apple_remote(), or get_joysticks().

Step 2: 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.

Step 3: Add event handlers to the device's controllers if desired.

Step 4: To start receiving events on the device, call Device.open(). After this time, you can begin querying the control values; the method will update them asynchronously.

Step 5: Call the method Device_close(), If you're done with the device.

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

Using the Joysticks

You must first detect and open a joystick before using it. Call pyglet.input.get_joysticks() to get a list of all joystick devices connected to a computer.

Syntax:

joysticks = pyglet.input.get joysticks()

Then, from the list, choose a joystick and call Joystick.

To open the device, use the following code:

if joysticks:
    joystick = joystick[0]
# calling the method to open the device
joystick.open()

Axis Parameters

In the x and y properties, the current location of the Joystick record is normalised values ranging from -1 to 1.

  1. For the x-axis
    • x = 1 indicates that the Joystick is pushed to the right.
    • b. x = -1 demonstrates that it is moved to the left. 
       
  2. For the y-axis:
    • y = 1 indicates that the Joystick is pushed up.
    • y = -1 means that the Joystick is pushed down.
       

Assume your Joystick has two actual controllers, with the second controller's location determined by rz and z, where rz represents the vertical axis and z represents the horizontal axis.

Status Change of Joystick

When the status of Joystick changes, it sends events to all open Joysticks. There's also the on_joybutton_press() function for buttons.

When any of the Joystick's buttons are pressed, the event is triggered as:

def on_joybutton_press(joysticks, button):
    pass

 

The on_joybutton_release() function releases the event which is sent whenever one of the Joystick's buttons is pressed:

def on_joybutton_release(joysticks, button):
    pass

Joystick Parameters

The joystick parameter is the Joystick instance whose buttons have changed state. It is useful if we have multiple joysticks connected. A button argument is merely an integer number representing the associated button's index in the buttons list.

The y and x characteristics can be used to check the current position of the Joystick in the majority of games. However, you should handle the on_joyaxis_motion() event if you want to receive notifications anytime the above variables change as follows:

def on_joyaxis_motion(joysticks, axis, value):
    pass

 

The Joystick parameter indicates which joystick device has been modified. The axis parameter is a string that indicates which axis changed data, such as rz, z, x, or y. The normalised value of the axis, ranging from 1 to -1, is given by value.

Motion Events

If the Joystick has a switch named hat, you can look at the hat_x and hat_y characteristics to see its current position value. The values are either 1, 0, or -1 for both. It's worth noting that hat y outputs -1 in the down position and 1 in the up position, which is the x-axis control's opposite.

To hold the on_joyhat_motion() event to be notified when the hat switch value changes; use the following syntax:

def on_joyhat_motion(joysticks, hat_x, hat_y):
    pass

 

The hat_x and hat_y parameters return the same values as the hat_x and hat-y characteristics on the Joystick.

 

The best approach to make use of the joystick event handlers is to include them in a controller class and then call:

joystick.push_handlers(my controllers)

 

Please keep in mind that updating the joystick button and axis values requires a running application event loop.

Using the Apple Remote

The Apple Remote is a small infrared remote that came with the iMac at first. There are six buttons on the remote:

  • Left
  • Right
  • Up
  • Down
  • Select
  • Menu

When a few buttons are held down, they also function as virtual buttons. These are referred to as right_hold, down_hold, select_hold etc.

Methods for Using Remote

As the first step call the get_apple_remote() method to use the remote. The code to use the get_apple_remote() method and then open it is as follows:

remote = pyglet.input.get_apple_remote()
# to open it
if remote:
    remote.open(window, exclusive=True)

 

Because the remote is in exclusive mode when we use it in our software, hitting the buttons on the remote does not operate Front Row or modify the volume on the computer.

 

When a button on the remote is pressed or released, the event handlers notify us:

def on_button_press(buttons):
    pass

 

The on_button_release() function releases the event which is sent whenever one of the remote's buttons is pressed as:

def on_button_release(buttons):
    pass

 

The button argument, which is the same string as the ten buttons names given above:

 left_hold, left, right_hold, right, select_hold, select, menu_hold, menu, and up and down, determines which button changed.

You can use the remote by defining event handlers in a controller class and then calling:

remote.push handlers(my controllers)

Frequently Asked Questions

Is the joystick a controller?

Yes! A Joystick is an input device that has been classified as a Game controller.

What is the history behind the name Joystick?

Joyce stick was possibly the earliest form, afterward slurred and compacted into joystick, according to several publications on aviation history citing a guy called Joyce as the creator.

In Pyglet, what is the apple remote?

The Apple remote is a compact white 6-button remote control that comes standard with most new Apple computers and laptops. We can use Mac OS X with the remote.

Conclusion

This article extensively discusses the working of pyglet with the keyboard and different types of keyboard events and defined key 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
Playing with the Mouse in Pyglet
Next article
Graphics in Pyglet
Live masterclass