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()

You can also try this code with Online Python Compiler
Run Code
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()

You can also try this code with Online Python Compiler
Run Code
Axis Parameters
In the x and y properties, the current location of the Joystick record is normalised values ranging from -1 to 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.
-
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

You can also try this code with Online Python Compiler
Run Code
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

You can also try this code with Online Python Compiler
Run Code
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

You can also try this code with Online Python Compiler
Run Code
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

You can also try this code with Online Python Compiler
Run Code
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)

You can also try this code with Online Python Compiler
Run Code
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)

You can also try this code with Online Python Compiler
Run Code
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

You can also try this code with Online Python Compiler
Run Code
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

You can also try this code with Online Python Compiler
Run Code
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)

You can also try this code with Online Python Compiler
Run Code
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, generators, Variables, Functions, 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!!