| 1 |
Controller & Joystick input |
| 2 |
=========================== |
| 3 |
|
| 4 |
The :py:mod:`~pyglet.input` module allows you to accept input from USB or Bluetooth |
| 5 |
human interface devices (HID). High-level classes are provided for working with |
| 6 |
game controllers, joysticks, and the Apple Remote, with named and normalized inputs. |
| 7 |
Basic support is also provided for Drawing Tablets, such as those made by Wacom. |
| 8 |
|
| 9 |
The game controller abstraction is most suitable for modern dual-analog stick controllers, |
| 10 |
such as those from video game consoles. The joystick abstraction is more generalized, |
| 11 |
and suits devices with an arbitrary number of buttons, absolute or relative axis, and hats. |
| 12 |
This includes devices like flight sticks, steering wheels, and just about anything else with |
| 13 |
digital and/or analog inputs. For most types of games, the game controller abstraction is |
| 14 |
recommended. |
| 15 |
|
| 16 |
For advanced use cases, it is also possible to access the low-level input devices directly. |
| 17 |
This can be useful if you need direct access to the raw inputs, without normalization. |
| 18 |
For most application and games this is not required. |
| 19 |
|
| 20 |
The :py:mod:`~pyglet.input` module provides several methods for querying |
| 21 |
devices, and a ControllerManager class to support hot-plugging of Controllers:: |
| 22 |
|
| 23 |
# get a list of all low-level input devices: |
| 24 |
devices = pyglet.input.get_devices() |
| 25 |
|
| 26 |
# get a list of all controllers: |
| 27 |
controllers = pyglet.input.get_controllers() |
| 28 |
|
| 29 |
# get a list of all joysticks: |
| 30 |
joysticks = pyglet.input.get_joysticks() |
| 31 |
|
| 32 |
# get a list of tablets: |
| 33 |
tablets = pyglet.input.get_tablets() |
| 34 |
|
| 35 |
# get an Apple Remote, if available: |
| 36 |
remote = pyglet.input.get_apple_remote() |
| 37 |
|
| 38 |
# create a ControllerManager instance: |
| 39 |
controller_manager = pyglet.input.ControllerManager() |
| 40 |
|
| 41 |
|
| 42 |
Using Controllers |
| 43 |
----------------- |
| 44 |
|
| 45 |
Controllers have a strictly defined set of inputs that mimic the layout of |
| 46 |
modern dual-analog stick video game console Controllers. This includes two |
| 47 |
analog sticks, analog triggers, a directional pad (dpad), face and shoulder |
| 48 |
buttons, and start/back/guide and stick press buttons. Many controllers also |
| 49 |
include the ability to play rumble effects (vibration). The following platform |
| 50 |
interfaces are used for Controller support: |
| 51 |
|
| 52 |
.. list-table:: |
| 53 |
:header-rows: 1 |
| 54 |
|
| 55 |
* - platform |
| 56 |
- interface |
| 57 |
- notes |
| 58 |
|
| 59 |
* - Linux |
| 60 |
- evdev |
| 61 |
- |
| 62 |
|
| 63 |
* - Windows |
| 64 |
- DirectInput & Xinput |
| 65 |
- rumble not implemented on DirectInput |
| 66 |
|
| 67 |
* - MacOSX |
| 68 |
- IOKit |
| 69 |
- rumble not yet implemented |
| 70 |
|
| 71 |
Before using a controller, you must find it and open it. You can either list |
| 72 |
and open Controllers manually, or use a :ref:`guide_controller-manager`. |
| 73 |
A ControllerManager provides useful events for easily handling hot-plugging |
| 74 |
of Controllers, which is described in a following section. First, however, |
| 75 |
lets look at how to do this manually. To get a list of all controllers currently |
| 76 |
connected to your computer, call pyglet.input.get_controllers():: |
| 77 |
|
| 78 |
controllers = pyglet.input.get_controllers() |
| 79 |
|
| 80 |
Then choose a controller from the list and call `Controller.open()` to open it:: |
| 81 |
|
| 82 |
if controllers: |
| 83 |
controller = controllers[0] |
| 84 |
controller.open() |
| 85 |
|
| 86 |
|
| 87 |
Once opened, you you can start receiving data from the the inputs. |
| 88 |
A variety of analog and digital :py:class:`~pyglet.input.Control` types |
| 89 |
are defined, which are automatically normalized to consistent ranges. The |
| 90 |
following analog controls are available: |
| 91 |
|
| 92 |
.. list-table:: |
| 93 |
:header-rows: 1 |
| 94 |
|
| 95 |
* - name |
| 96 |
- type |
| 97 |
- range |
| 98 |
|
| 99 |
* - leftx |
| 100 |
- float |
| 101 |
- -1~1 |
| 102 |
|
| 103 |
* - lefty |
| 104 |
- float |
| 105 |
- -1~1 |
| 106 |
|
| 107 |
* - rightx |
| 108 |
- float |
| 109 |
- -1~1 |
| 110 |
|
| 111 |
* - righty |
| 112 |
- float |
| 113 |
- -1~1 |
| 114 |
|
| 115 |
* - dpadx |
| 116 |
- float |
| 117 |
- -1~1 |
| 118 |
|
| 119 |
* - dpady |
| 120 |
- float |
| 121 |
- -1~1 |
| 122 |
|
| 123 |
* - lefttrigger |
| 124 |
- float |
| 125 |
- 0~1 |
| 126 |
|
| 127 |
* - righttrigger |
| 128 |
- float |
| 129 |
- 0~1 |
| 130 |
|
| 131 |
The following digital controls are available: |
| 132 |
|
| 133 |
.. list-table:: |
| 134 |
:header-rows: 1 |
| 135 |
|
| 136 |
* - Name |
| 137 |
- notes |
| 138 |
* - a |
| 139 |
- the "south" face button |
| 140 |
* - b |
| 141 |
- the "east" face button |
| 142 |
* - x |
| 143 |
- the "west" face button |
| 144 |
* - y |
| 145 |
- the "north" face button |
| 146 |
* - leftshoulder |
| 147 |
- |
| 148 |
* - rightshoulder |
| 149 |
- |
| 150 |
* - start |
| 151 |
- called "options" on some controllers |
| 152 |
* - back |
| 153 |
- called "select" or "share" on some controllers |
| 154 |
* - guide |
| 155 |
- usually in the center, with a company logo |
| 156 |
* - leftstick |
| 157 |
- pressing in on the left analog stick |
| 158 |
* - rightstick |
| 159 |
- pressing in on the right analog stick |
| 160 |
|
| 161 |
|
| 162 |
These values can be read in two ways. First, you can just query them manually |
| 163 |
in your game loop. All control names listed above are properties on the |
| 164 |
controller instance:: |
| 165 |
|
| 166 |
# controller_instance.a (boolean) |
| 167 |
# controller_instance.leftx (float) |
| 168 |
|
| 169 |
if controller_instance.a == True: |
| 170 |
# do something |
| 171 |
|
| 172 |
|
| 173 |
Alternatively, since controllers are a subclass of :py:class:`~pyglet.event.EventDispatcher`, |
| 174 |
events will be dispatched when any of the values change. This is usually the |
| 175 |
recommended way to handle input, since it reduces the chance of "missed" button |
| 176 |
presses due to slow polling. The different controls are grouped into the following |
| 177 |
event types: |
| 178 |
|
| 179 |
.. list-table:: |
| 180 |
:header-rows: 1 |
| 181 |
|
| 182 |
* - Event |
| 183 |
- Arguments |
| 184 |
- types |
| 185 |
|
| 186 |
* - on_button_press |
| 187 |
- controller, button_name |
| 188 |
- :py:class:`~pyglet.input.Controller`, `str` |
| 189 |
|
| 190 |
* - on_button_release |
| 191 |
- controller, button_name |
| 192 |
- :py:class:`~pyglet.input.Controller`, `str` |
| 193 |
|
| 194 |
* - on_stick_motion |
| 195 |
- controller, stick_name, vector |
| 196 |
- :py:class:`~pyglet.input.Controller`, `str`, :py:class:`~pyglet.math.Vec2` |
| 197 |
|
| 198 |
* - on_dpad_motion |
| 199 |
- controller, left, right, up, down |
| 200 |
- :py:class:`~pyglet.input.Controller`, :py:class:`~pyglet.math.Vec2` |
| 201 |
|
| 202 |
* - on_trigger_motion |
| 203 |
- controller, trigger_name, value |
| 204 |
- :py:class:`~pyglet.input.Controller`, `str`, `float` |
| 205 |
|
| 206 |
|
| 207 |
Analog (and Dpad) events can be handled like this:: |
| 208 |
|
| 209 |
@controller.event |
| 210 |
def on_stick_motion(controller, name, vector): |
| 211 |
|
| 212 |
if name == "leftstick": |
| 213 |
# Do something with the 2D vector |
| 214 |
elif name == "rightstick": |
| 215 |
# Do something with the 2D vector |
| 216 |
|
| 217 |
@controller.event |
| 218 |
def on_trigger_motion(controller, name, value): |
| 219 |
|
| 220 |
if name == "lefttrigger": |
| 221 |
# Do something with the value |
| 222 |
elif name == "righttrigger": |
| 223 |
# Do something with the value |
| 224 |
|
| 225 |
@controller.event |
| 226 |
def on_dpad_motion(controller, vector): |
| 227 |
# Do something with the 2D vector |
| 228 |
|
| 229 |
Digital events can be handled like this:: |
| 230 |
|
| 231 |
@controller.event |
| 232 |
def on_button_press(controller, button_name): |
| 233 |
if button_name == 'a': |
| 234 |
# start firing |
| 235 |
elif button_name == 'b': |
| 236 |
# do something else |
| 237 |
|
| 238 |
|
| 239 |
@controller.event |
| 240 |
def on_button_release(controller, button_name): |
| 241 |
if button_name == 'a': |
| 242 |
# stop firing |
| 243 |
elif button_name == 'b': |
| 244 |
# do something else |
| 245 |
|
| 246 |
|
| 247 |
Rumble |
| 248 |
^^^^^^ |
| 249 |
|
| 250 |
Many controllers also support playing rumble (vibration) effects. There |
| 251 |
are both strong and weak effects, which can be played independently:: |
| 252 |
|
| 253 |
controller.rumble_play_weak(strength, duration=0.5) |
| 254 |
controller.rumble_play_strong(strength, duration=0.5) |
| 255 |
|
| 256 |
The `strength` parameter should be on a scale of 0-1. Values outside of |
| 257 |
this range will be clamped. The optional `duration` parameter is in seconds. |
| 258 |
The maximum duration can vary from platform to platform, but is usually |
| 259 |
at least 5 seconds. If you play another effect while an existing effect is |
| 260 |
still playing, it will replace it. You can also stop playback of a rumble |
| 261 |
effect at any time:: |
| 262 |
|
| 263 |
controller.rumble_stop_weak() |
| 264 |
controller.rumble_stop_strong() |
| 265 |
|
| 266 |
|
| 267 |
.. _guide_controller-manager: |
| 268 |
|
| 269 |
ControllerManager |
| 270 |
^^^^^^^^^^^^^^^^^ |
| 271 |
|
| 272 |
To simplify hot-plugging of Controllers, the :py:class:`~pyglet.input.ControllerManager` |
| 273 |
class is available. This class has a `get_controllers()` method to be used |
| 274 |
in place of `pyglet.input.get_controllers()`. There are also `on_connect` |
| 275 |
and `on_disconnect` events, which dispatch a Controller instance whenever one |
| 276 |
is connected or disconnected. First lets review the basic functionality. |
| 277 |
|
| 278 |
To use a ControllerManager, first create an instance:: |
| 279 |
|
| 280 |
manager = pyglet.input.ControllerManager() |
| 281 |
|
| 282 |
You can then query the currently connected controllers from this instance. |
| 283 |
(An empty list is returned if no controllers are detected):: |
| 284 |
|
| 285 |
controllers = manager.get_controllers() |
| 286 |
|
| 287 |
Choose a controller from the list and call `Controller.open()` to open it:: |
| 288 |
|
| 289 |
if controllers: |
| 290 |
controller = controllers[0] |
| 291 |
controller.open() |
| 292 |
|
| 293 |
To handle controller connections, attach handlers to the following methods:: |
| 294 |
|
| 295 |
@manager.event |
| 296 |
def on_connect(controller): |
| 297 |
print(f"Connected: {controller}") |
| 298 |
|
| 299 |
@manager.event |
| 300 |
def on_disconnect(controller): |
| 301 |
print(f"Disconnected: {controller}") |
| 302 |
|
| 303 |
|
| 304 |
Those are the basics, and provide the building blocks necessary to implement |
| 305 |
hot-plugging of Controllers in your game. For an example of bringing these |
| 306 |
concepts together, have a look at ``examples/input/controller.py`` in the |
| 307 |
repository. |
| 308 |
|
| 309 |
.. note:: If you are using a ControllerManager, then you should not use |
| 310 |
`pyglet.input.get_controllers()` directly. The results are |
| 311 |
undefined. Use `ControllerManager.get_controllers()` instead. |
| 312 |
|
| 313 |
|
| 314 |
Using Joysticks |
| 315 |
--------------- |
| 316 |
|
| 317 |
Before using a joystick, you must find it and open it. To get a list |
| 318 |
of all joystick devices currently connected to your computer, call |
| 319 |
:py:func:`pyglet.input.get_joysticks`:: |
| 320 |
|
| 321 |
joysticks = pyglet.input.get_joysticks() |
| 322 |
|
| 323 |
Then choose a joystick from the list and call `Joystick.open` to open |
| 324 |
the device:: |
| 325 |
|
| 326 |
if joysticks: |
| 327 |
joystick = joysticks[0] |
| 328 |
joystick.open() |
| 329 |
|
| 330 |
The current position of the joystick is recorded in its 'x' and 'y' |
| 331 |
attributes, both of which are normalized to values within the range |
| 332 |
of -1 to 1. For the x-axis, `x` = -1 means the joystick is pushed |
| 333 |
all the way to the left and `x` = 1 means the joystick is pushed to the right. |
| 334 |
For the y-axis, a value of `y` = -1 means that the joystick is pushed up |
| 335 |
and a value of `y` = 1 means that the joystick is pushed down. If other |
| 336 |
axis exist, they will be labeled `z`, `rx`, `ry`, or `rz`. |
| 337 |
|
| 338 |
The state of the joystick buttons is contained in the `buttons` |
| 339 |
attribute as a list of boolean values. A True value indicates that |
| 340 |
the corresponding button is being pressed. While buttons may be |
| 341 |
labeled A, B, X, or Y on the physical joystick, they are simply |
| 342 |
referred to by their index when accessing the `buttons` list. There |
| 343 |
is no easy way to know which button index corresponds to which |
| 344 |
physical button on the device without testing the particular joystick, |
| 345 |
so it is a good idea to let users change button assignments. |
| 346 |
|
| 347 |
Each open joystick dispatches events when the joystick changes state. |
| 348 |
For buttons, there is the :py:meth:`~pyglet.input.Joystick.on_joybutton_press` |
| 349 |
event which is sent whenever any of the joystick's buttons are pressed:: |
| 350 |
|
| 351 |
def on_joybutton_press(joystick, button): |
| 352 |
pass |
| 353 |
|
| 354 |
and the :py:meth:`~pyglet.input.Joystick.on_joybutton_release` event which is |
| 355 |
sent whenever any of the joystick's buttons are released:: |
| 356 |
|
| 357 |
def on_joybutton_release(joystick, button): |
| 358 |
pass |
| 359 |
|
| 360 |
The :py:class:`~pyglet.input.Joystick` parameter is the |
| 361 |
:py:class:`~pyglet.input.Joystick` instance whose buttons changed state |
| 362 |
(useful if you have multiple joysticks connected). |
| 363 |
The `button` parameter signifies which button changed and is simply an |
| 364 |
integer value, the index of the corresponding button in the `buttons` |
| 365 |
list. |
| 366 |
|
| 367 |
For most games, it is probably best to examine the current position of |
| 368 |
the joystick directly by using the `x` and `y` attributes. However if |
| 369 |
you want to receive notifications whenever these values change you |
| 370 |
should handle the :py:meth:`~pyglet.input.Joystick.on_joyaxis_motion` event:: |
| 371 |
|
| 372 |
def on_joyaxis_motion(joystick, axis, value): |
| 373 |
pass |
| 374 |
|
| 375 |
The :py:class:`~pyglet.input.Joystick` parameter again tells you which |
| 376 |
joystick device changed. The `axis` parameter is string such as |
| 377 |
"x", "y", or "rx" telling you which axis changed value. And `value` |
| 378 |
gives the current normalized value of the axis, ranging between -1 and 1. |
| 379 |
|
| 380 |
If the joystick has a hat switch, you may examine its current value by |
| 381 |
looking at the `hat_x` and `hat_y` attributes. For both, the values |
| 382 |
are either -1, 0, or 1. Note that `hat_y` will output 1 in the up |
| 383 |
position and -1 in the down position, which is the opposite of the |
| 384 |
y-axis control. |
| 385 |
|
| 386 |
To be notified when the hat switch changes value, handle the |
| 387 |
:py:meth:`~pyglet.input.Joystick.on_joyhat_motion` event:: |
| 388 |
|
| 389 |
def on_joyhat_motion(joystick, hat_x, hat_y): |
| 390 |
pass |
| 391 |
|
| 392 |
The `hat_x` and `hat_y` parameters give the same values as the |
| 393 |
joystick's `hat_x` and `hat_y` attributes. |
| 394 |
|
| 395 |
A good way to use the joystick event handlers might be to define them |
| 396 |
within a controller class and then call:: |
| 397 |
|
| 398 |
joystick.push_handlers(my_controller) |
| 399 |
|
| 400 |
Please note that you need a running application event loop for the joystick |
| 401 |
button an axis values to be properly updated. See the |
| 402 |
:ref:`programming-guide-eventloop` section for more details on how to start |
| 403 |
an event loop. |
| 404 |
|
| 405 |
|
| 406 |
Using the Apple Remote |
| 407 |
---------------------- |
| 408 |
|
| 409 |
The Apple Remote is a small infrared remote originally distributed |
| 410 |
with the iMac. The remote has six buttons, which are accessed with |
| 411 |
the names `left`, `right`, `up`, `down`, `menu`, and `select`. |
| 412 |
Additionally when certain buttons are held down, they act as virtual |
| 413 |
buttons. These are named `left_hold`, `right_hold`, `menu_hold`, and |
| 414 |
`select_hold`. |
| 415 |
|
| 416 |
To use the remote, first call :py:func:`~pyglet.input.get_apple_remote`:: |
| 417 |
|
| 418 |
remote = pyglet.input.get_apple_remote() |
| 419 |
|
| 420 |
Then open it:: |
| 421 |
|
| 422 |
if remote: |
| 423 |
remote.open(window, exclusive=True) |
| 424 |
|
| 425 |
The remote is opened in exclusive mode so that while we are using the |
| 426 |
remote in our program, pressing the buttons does not activate Front |
| 427 |
Row, or change the volume, etc. on the computer. |
| 428 |
|
| 429 |
The following event handlers tell you when a button on the remote has |
| 430 |
been either pressed or released:: |
| 431 |
|
| 432 |
def on_button_press(button): |
| 433 |
pass |
| 434 |
|
| 435 |
def on_button_release(button): |
| 436 |
pass |
| 437 |
|
| 438 |
The `button` parameter indicates which button changed and is a string |
| 439 |
equal to one of the ten button names defined above: "up", "down", |
| 440 |
"left", "left_hold", "right", "right_hold", "select", "select_hold", |
| 441 |
"menu", or "menu_hold". |
| 442 |
|
| 443 |
To use the remote, you may define code for the event handlers in |
| 444 |
some controller class and then call:: |
| 445 |
|
| 446 |
remote.push_handlers(my_controller) |
| 447 |
|
| 448 |
|
| 449 |
Low-level Devices |
| 450 |
----------------- |
| 451 |
|
| 452 |
It's usually easier to use the high-level interfaces but, for specialized |
| 453 |
hardware, the low-level device can be accessed directly. You can query the |
| 454 |
list of all devices, and check the `name` attribute to find the correct |
| 455 |
device:: |
| 456 |
|
| 457 |
for device in pyglet.input.get_devices(): |
| 458 |
print(device.name) |
| 459 |
|
| 460 |
After identifying the Device you wish to use, you must first open it:: |
| 461 |
|
| 462 |
device.open() |
| 463 |
|
| 464 |
Devices contain a list of :py:class:`~pyglet.input.Control` objects. |
| 465 |
There are three types of controls: :py:class:`~pyglet.input.Button`, |
| 466 |
:py:class:`~pyglet.input.AbsoluteAxis`, and :py:class:`~pyglet.input.RelativeAxis`. |
| 467 |
For helping identify individual controls, each control has at least a |
| 468 |
`name`, and optionally a `raw_name` attribute. Control values can by |
| 469 |
queried at any time by checking the `Control.value` property. In addition, |
| 470 |
every control is also a subclass of :py:class:`~pyglet.event.EventDispatcher`, |
| 471 |
so you can add handlers to receive changes as well. All Controls dispatch the |
| 472 |
`on_change` event. Buttons also dispatch `on_press` and `on_release` events.:: |
| 473 |
|
| 474 |
# All controls: |
| 475 |
|
| 476 |
@control.event |
| 477 |
def on_change(value): |
| 478 |
print("value:", value) |
| 479 |
|
| 480 |
# Buttons: |
| 481 |
|
| 482 |
@control.event |
| 483 |
def on_press(): |
| 484 |
print("button pressed") |
| 485 |
|
| 486 |
@control.event |
| 487 |
def on_release(): |
| 488 |
print("button release") |