pyglet.input

Joystick, Game Controller, Tablet and USB HID device support.

This module provides a unified interface to almost any input device, besides the regular mouse and keyboard support provided by Window. At the lowest level, get_devices() can be used to retrieve a list of all supported devices, including joysticks, tablets, game controllers, wheels, pedals, remote controls, keyboards and mice. The set of returned devices varies greatly depending on the operating system (and, of course, what’s plugged in).

At this level pyglet does not try to interpret what a particular device is, merely what controls it provides. A Control can be either a button, whose value is either True or False, or a relative or absolute-valued axis, whose value is a float. Sometimes the name of a control can be provided (for example, x, representing the horizontal axis of a joystick), but often not. In these cases the device API may still be useful – the user will have to be asked to press each button in turn or move each axis separately to identify them.

Higher-level interfaces are provided for joysticks, game controllers, tablets and the Apple remote control. These devices can usually be identified by pyglet positively, and a base level of functionality for each one provided through a common interface.

To use an input device:

  1. Call get_devices(), get_apple_remote(), get_controllers() or get_joysticks() to retrieve and identify the device.

  2. For low-level devices (retrieved by get_devices()), query the devices list of controls and determine which ones you are interested in. For high-level interfaces the set of controls is provided by the interface.

  3. Optionally attach event handlers to controls on the device. For high-level interfaces, additional events are available.

  4. Call Device.open() to begin receiving events on the device. You can begin querying the control values after this time; they will be updated asynchronously.

  5. Call Device.close() when you are finished with the device (not needed if your application quits at this time).

To use a tablet, follow the procedure above using get_tablets(), but note that no control list is available; instead, calling Tablet.open() returns a TabletCanvas onto which you should set your event handlers.

For game controllers, the ControllerManager is available. This provides a convenient way to handle hot-plugging of controllers.

New in version 1.2.

Classes

class ControllerManager

High level interface for managing game Controllers.

This class provides a convenient way to handle the connection and disconnection of devices. A list of all connected Controllers can be queried at any time with the get_controllers method. For hot-plugging, events are dispatched for on_connect and on_disconnect. To use the ControllerManager, first make an instance:

controller_man = pyglet.input.ControllerManager()

At the start of your game, query for any Controllers that are already connected:

controllers = controller_man.get_controllers()

To handle Controllers that are connected or disconnected after the start of your game, register handlers for the appropriate events:

@controller_man.event
def on_connect(controller):
    # code to handle newly connected
    # (or re-connected) controllers
    controller.open()
    print("Connect:", controller)

@controller_man.event
def on_disconnect(controller):
    # code to handle disconnected Controller
    print("Disconnect:", controller)

New in version 1.2.

Methods

get_controllers() list[pyglet.input.base.Controller]

Get a list of all connected Controllers

Return type:

list[Controller]

Events

on_connect(controller) Controller

A Controller has been connected. If this is a previously dissconnected Controller that is being re-connected, the same Controller instance will be returned.

Return type:

Controller

on_disconnect(controller) Controller

A Controller has been disconnected.

Return type:

Controller

__init__()
__new__(**kwargs)
class Device

Bases: object

Low level input device.

__init__(display: Display, name: str) None

Create a Device to receive input from.

Parameters:
  • display (Display) – The Display this device is connected to.

  • name (str) – The name of the device, as described by the device firmware.

close() None

Close the device.

Return type:

None

get_controls() list[pyglet.input.base.Control]

Get a list of controls provided by the device.

Return type:

list[Control]

get_guid() str

Get the device GUID, in SDL2 format.

Return a str containing a unique device identification string. This is generated from the hardware identifiers, and is in the same format as was popularized by SDL2. GUIDs differ between platforms, but are generally 32 hexidecimal characters.

Return type:

str

open(
window: None | Window = None,
exclusive: bool = False,
) None

Open the device to begin receiving input from it.

Parameters:
  • window (None | Window) – Optional window to associate with the device. The behaviour of this parameter is device and operating system dependant. It can usually be omitted for most devices.

  • exclusive (bool) – If True the device will be opened exclusively so that no other application can use it.

Raises:

DeviceOpenException – If the device cannot be opened in exclusive mode, usually due to being opened exclusively by another application.

Return type:

None

property is_open: bool
manufacturer: str | None

The manufacturer name, if available

class Control

Bases: EventDispatcher

Single value input provided by a device.

A control’s value can be queried when the device is open. Event handlers can be attached to the control to be called when the value changes.

The min and max properties are provided as advertised by the device; in some cases the control’s value will be outside this range.

Events

on_change(value) float

The value changed.

Return type:

float

Attributes

value

Current value of the control.

The range of the value is device-dependent; for absolute controls the range is given by min and max (however the value may exceed this range); for relative controls the range is undefined.

__init__(
name: None | str,
raw_name: None | str = None,
inverted: bool = False,
)

Create a Control to receive input.

Parameters:
  • name (None | str) – The name of the control, or None if unknown.

  • raw_name (None | str) – Optional unmodified name of the control, as presented by the operating system; or None if unknown.

  • inverted (bool) – If True, the value reported is actually inverted from what the device reported; usually this is to provide consistency across operating systems.

__new__(**kwargs)
class RelativeAxis

Bases: Control

An axis whose value represents a relative change from the previous value.

This type of axis is used for controls that can scroll or move continuously, such as a scrolling or pointing input. The value is read as a delta change from the previous value.

RX: str = 'rx'
RY: str = 'ry'
RZ: str = 'rz'
WHEEL: str = 'wheel'
X: str = 'x'
Y: str = 'y'
Z: str = 'z'
property value: float

Current value of the control.

The range of the value is device-dependent; for absolute controls the range is given by min and max (however the value may exceed this range); for relative controls the range is undefined.

class AbsoluteAxis

Bases: Control

An axis whose value represents the current measurement from the device.

This type of axis is used for controls that have minimum and maximum positions. The value is a range between the min and max.

__init__(
name: str,
minimum: float,
maximum: float,
raw_name: None | str = None,
inverted: bool = False,
)

Create a Control to receive input.

Parameters:
  • name (str) – The name of the control, or None if unknown.

  • raw_name (None | str) – Optional unmodified name of the control, as presented by the operating system; or None if unknown.

  • inverted (bool) – If True, the value reported is actually inverted from what the device reported; usually this is to provide consistency across operating systems.

HAT: str = 'hat'
HAT_X: str = 'hat_x'
HAT_Y: str = 'hat_y'
RX: str = 'rx'
RY: str = 'ry'
RZ: str = 'rz'
X: str = 'x'
Y: str = 'y'
Z: str = 'z'
class Button

Bases: Control

A control whose value is boolean.

Events

on_press()

The button was pressed.

on_release()

The button was released.

Attributes

value
__init__(
name: None | str,
raw_name: None | str = None,
inverted: bool = False,
)

Create a Control to receive input.

Parameters:
  • name (None | str) – The name of the control, or None if unknown.

  • raw_name (None | str) – Optional unmodified name of the control, as presented by the operating system; or None if unknown.

  • inverted (bool) – If True, the value reported is actually inverted from what the device reported; usually this is to provide consistency across operating systems.

__new__(**kwargs)
class Controller

Bases: EventDispatcher

High-level interface for Game Controllers.

Unlike Joysticks, Controllers have a strictly defined set of inputs that matches the layout of popular home video game console Controllers. This includes a variety of face and shoulder buttons, analog sticks and triggers, a directional pad, and optional rumble (force feedback) effects.

To use a Controller, you must first call open. Controllers will then dispatch various events whenever the inputs change. They can also be polled manually at any time to find the current value of any inputs. Analog stick inputs are normalized to the range [-1.0, 1.0], and triggers are normalized to the range [0.0, 1.0]. All other inputs are digital.

Note: A running application event loop is required

The following event types are dispatched:

on_button_press on_button_release on_stick_motion on_dpad_motion on_trigger_motion

Methods

open(
window: None | Window = None,
exclusive: bool = False,
) None

Open the controller. See Device.open.

Return type:

None

close() None

Close the controller. See Device.close.

Return type:

None

Events

on_stick_motion(
controller: Controller,
stick: str,
xvalue: float,
yvalue: float,
)

The value of a controller analogue stick changed.

Parameters:
  • controller (Controller) – The controller whose analogue stick changed.

  • stick (str) – The name of the stick that changed.

  • xvalue (float) – The current X axis value, normalized to [-1, 1].

  • yvalue (float) – The current Y axis value, normalized to [-1, 1].

on_dpad_motion(
controller: Controller,
dpleft: bool,
dpright: bool,
dpup: bool,
dpdown: bool,
)

The direction pad of the controller changed.

Parameters:
  • controller (Controller) – The controller whose hat control changed.

  • dpleft (bool) – True if left is pressed on the directional pad.

  • dpright (bool) – True if right is pressed on the directional pad.

  • dpup (bool) – True if up is pressed on the directional pad.

  • dpdown (bool) – True if down is pressed on the directional pad.

on_trigger_motion(
controller: Controller,
trigger: str,
value: float,
)

The value of a controller analogue stick changed.

Parameters:
  • controller (Controller) – The controller whose analogue stick changed.

  • trigger (str) – The name of the trigger that changed.

  • value (float) – The current value of the trigger, normalized to [0, 1].

on_button_press(controller: Controller, button: str)

A button on the controller was pressed.

Parameters:
  • controller (Controller) – The controller whose button was pressed.

  • button (str) – The name of the button that was pressed.

on_button_release(controller: Controller, button: str)

A button on the joystick was released.

Parameters:
  • controller (Controller) – The controller whose button was released.

  • button (str) – The name of the button that was released.

__init__(device: Device, mapping: dict)

Create a Controller instace mapped to a Device.

New in version 2.0.

__new__(**kwargs)
class Joystick

Bases: EventDispatcher

High-level interface for joystick-like devices. This includes a wide range of analog and digital joysticks, gamepads, controllers, and possibly even steering wheels and other input devices. There is unfortunately no easy way to distinguish between most of these different device types.

For a simplified subset of Joysticks, see the Controller interface. This covers a variety of popular game console controllers. Unlike Joysticks, Controllers have strictly defined layouts and inputs.

To use a joystick, first call open, then in your game loop examine the values of x, y, and so on. These values are normalized to the range [-1.0, 1.0].

To receive events when the value of an axis changes, attach an on_joyaxis_motion event handler to the joystick. The Joystick instance, axis name, and current value are passed as parameters to this event.

To handle button events, you should attach on_joybutton_press and on_joy_button_release event handlers to the joystick. Both the Joystick instance and the index of the changed button are passed as parameters to these events.

Alternately, you may attach event handlers to each individual button in button_controls to receive on_press or on_release events.

To use the hat switch, attach an on_joyhat_motion event handler to the joystick. The handler will be called with both the hat_x and hat_y values whenever the value of the hat switch changes.

The device name can be queried to get the name of the joystick.

Methods

open(window: None | Window, exclusive: bool = False) None

Open the joystick device. See Device.open.

Return type:

None

close() None

Close the joystick device. See Device.close.

Return type:

None

Events

on_joyaxis_motion(
joystick: Joystick,
axis: str,
value: float,
)

The value of a joystick axis changed.

Parameters:
  • joystick (Joystick) – The joystick device whose axis changed.

  • axis (str) – The name of the axis that changed.

  • value (float) – The current value of the axis, normalized to [-1, 1].

on_joyhat_motion(
joystick: Joystick,
hat_x: float,
hat_y: float,
)

The value of the joystick hat switch changed.

Parameters:
  • joystick (Joystick) – The joystick device whose hat control changed.

  • hat_x (float) – Current hat (POV) horizontal position; one of -1.0 (left), 0.0 (centered) or 1.0 (right).

  • hat_y (float) – Current hat (POV) vertical position; one of -1.0 (bottom), 0.0 (centered) or 1.0 (top).

on_joybutton_press(joystick: Joystick, button: int)

A button on the joystick was pressed.

Parameters:
  • joystick (Joystick) – The joystick device whose button was pressed.

  • button (int) – The index (in button_controls) of the button that was pressed.

on_joybutton_release(joystick: Joystick, button: int)

A button on the joystick was released.

Parameters:
  • joystick (Joystick) – The joystick device whose button was released.

  • button (int) – The index (in button_controls) of the button that was released.

__init__(device)
__new__(**kwargs)
class AppleRemote

Bases: EventDispatcher

High-level interface for Apple remote control.

This interface provides access to the 6 button controls on the remote. Pressing and holding certain buttons on the remote is interpreted as a separate control.

Methods

open(window: Window, exclusive: bool = False)

Open the device. See Device.open.

close()

Close the device. See Device.close.

Events

on_button_press(button: str)

A button on the remote was pressed.

Only the ‘up’ and ‘down’ buttons will generate an event when the button is first pressed. All other buttons on the remote will wait until the button is released and then send both the press and release events at the same time.

Parameters:

button (str) – The name of the button that was pressed. The valid names are ‘up’, ‘down’, ‘left’, ‘right’, ‘left_hold’, ‘right_hold’, ‘menu’, ‘menu_hold’, ‘select’, and ‘select_hold’

on_button_release(button: str)

A button on the remote was released.

The ‘select_hold’ and ‘menu_hold’ button release events are sent immediately after the corresponding press events regardless of whether the user has released the button.

Parameters:

button (str) – The name of the button that was released. The valid names are ‘up’, ‘down’, ‘left’, ‘right’, ‘left_hold’, ‘right_hold’, ‘menu’, ‘menu_hold’, ‘select’, and ‘select_hold’

__init__(device)
__new__(**kwargs)
class Tablet

High-level interface to tablet devices.

Unlike other devices, tablets must be opened for a specific window, and cannot be opened exclusively. The open method returns a TabletCanvas object, which supports the events provided by the tablet.

Currently only one tablet device can be used, though it can be opened on multiple windows. If more than one tablet is connected, the behaviour is undefined.

__init__()
__new__(**kwargs)

Functions

get_apple_remote(display: None | Display = None) AppleRemote | None

Get the Apple remote control device, if it exists.

The Apple remote is the small white 6-button remote control that accompanies most recent Apple desktops and laptops. The remote can only be used with Mac OS X.

Parameters:

display (None | Display) – Currently ignored.

Return type:

AppleRemote | None

get_devices(
display: None | Display = None,
) list[pyglet.input.base.Device]

Get a list of all attached input devices.

Parameters:

display (None | Display) – The display device to query for input devices. Ignored on Mac OS X and Windows. On Linux, defaults to the default display device.

Return type:

list[Device]

get_controllers(
display: None | Display = None,
) list[pyglet.input.base.Controller]

Get a list of attached controllers.

Parameters:

display (None | Display) – The display device to query for input devices. Ignored on Mac OS X and Windows. On Linux, defaults to the default display device.

Return type:

list[Controller]

get_joysticks(
display: None | Display = None,
) list[pyglet.input.base.Joystick]

Get a list of attached joysticks.

Parameters:

display (None | Display) – The display device to query for input devices. Ignored on Mac OS X and Windows. On Linux, defaults to the default display device.

Return type:

list[Joystick]

get_tablets(
display: None | Display = None,
) list[pyglet.input.base.Tablet]

Get a list of tablets.

This function may return a valid tablet device even if one is not attached (for example, it is not possible on Mac OS X to determine if a tablet device is connected). Despite returning a list of tablets, pyglet does not currently support multiple tablets, and the behaviour is undefined if more than one is attached.

Parameters:

display (None | Display) – The display device to query for input devices. Ignored on Mac OS X and Windows. On Linux, defaults to the default display device.

Return type:

list[Tablet]

Exceptions

class DeviceException
class DeviceOpenException
class DeviceExclusiveException