Difficult-Rocket/libs/pyglet/input/__init__.py

176 lines
6.9 KiB
Python
Raw Normal View History

2022-04-30 13:56:57 +08:00
"""Joystick, Game Controller, Tablet and USB HID device support.
2021-04-16 23:21:06 +08:00
This module provides a unified interface to almost any input device, besides
the regular mouse and keyboard support provided by
:py:class:`~pyglet.window.Window`. At the lowest
level, :py:func:`get_devices` can be used to retrieve a list of all supported
devices, including joysticks, tablets, space 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 :py:class:`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.
2022-04-30 13:56:57 +08:00
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.
2021-04-16 23:21:06 +08:00
To use an input device:
2022-04-30 13:56:57 +08:00
1. Call :py:func:`get_devices`, :py:func:`get_apple_remote`,
:py:func:`get_controllers` or :py:func:`get_joysticks` to retrieve and
identify the device.
2021-04-16 23:21:06 +08:00
2. For low-level devices (retrieved by :py:func:`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.
2022-04-30 13:56:57 +08:00
3. Optionally attach event handlers to controls on the device. For high-level
interfaces, additional events are available.
2021-04-16 23:21:06 +08:00
4. Call :py:meth:`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 :py:meth:`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 :py:func:`get_tablets`, but
note that no control list is available; instead, calling :py:meth:`Tablet.open`
returns a :py:class:`TabletCanvas` onto which you should set your event
handlers.
2022-04-30 13:56:57 +08:00
For game controllers, the :py:class:`ControllerManager` is available. This
provides a convenient way to handle hot-plugging of controllers.
2021-04-16 23:21:06 +08:00
.. versionadded:: 1.2
"""
import sys
2022-04-30 13:56:57 +08:00
from .base import Device, Control, RelativeAxis, AbsoluteAxis, ControllerManager
2022-03-05 23:10:18 +08:00
from .base import Button, Joystick, AppleRemote, Tablet, Controller
2021-04-16 23:21:06 +08:00
from .base import DeviceException, DeviceOpenException, DeviceExclusiveException
2021-04-16 23:21:06 +08:00
_is_pyglet_doc_run = hasattr(sys, "is_pyglet_doc_run") and sys.is_pyglet_doc_run
def get_apple_remote(display=None):
"""Get the Apple remote control device.
2021-04-16 23:21:06 +08:00
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 : `~pyglet.canvas.Display`
Currently ignored.
:rtype: AppleRemote
:return: The remote device, or `None` if the computer does not support it.
"""
return None
2021-04-16 23:21:06 +08:00
if _is_pyglet_doc_run:
def get_devices(display=None):
"""Get a list of all attached input devices.
:Parameters:
display : `~pyglet.canvas.Display`
The display device to query for input devices. Ignored on Mac
OS X and Windows. On Linux, defaults to the default display
device.
:rtype: list of :py:class:`Device`
"""
def get_joysticks(display=None):
"""Get a list of attached joysticks.
:Parameters:
display : `~pyglet.canvas.Display`
The display device to query for input devices. Ignored on Mac
OS X and Windows. On Linux, defaults to the default display
device.
:rtype: list of :py:class:`Joystick`
"""
2022-03-05 23:10:18 +08:00
def get_controllers(display=None):
"""Get a list of attached controllers.
:Parameters:
display : `~pyglet.canvas.Display`
The display device to query for input devices. Ignored on Mac
OS X and Windows. On Linux, defaults to the default display
device.
2022-03-05 23:10:18 +08:00
:rtype: list of :py:class:`Controller`
"""
2021-04-16 23:21:06 +08:00
def get_tablets(display=None):
"""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 : `~pyglet.canvas.Display`
The display device to query for input devices. Ignored on Mac
OS X and Windows. On Linux, defaults to the default display
device.
:rtype: list of :py:class:`Tablet`
"""
else:
def get_tablets(display=None):
return []
2022-04-30 13:56:57 +08:00
from pyglet import compat_platform, options
2021-04-16 23:21:06 +08:00
if compat_platform.startswith('linux'):
from .x11_xinput_tablet import get_tablets
2022-04-30 13:56:57 +08:00
from .x11_xinput import get_devices as x11xinput_get_devices
2021-04-16 23:21:06 +08:00
from .evdev import get_devices as evdev_get_devices
from .evdev import get_joysticks
2022-03-05 23:10:18 +08:00
from .evdev import get_controllers
2022-04-30 13:56:57 +08:00
from .evdev import EvdevControllerManager as ControllerManager
2021-04-16 23:21:06 +08:00
def get_devices(display=None):
2022-04-30 13:56:57 +08:00
return evdev_get_devices(display) + x11xinput_get_devices(display)
2021-04-16 23:21:06 +08:00
elif compat_platform in ('cygwin', 'win32'):
2022-04-30 13:56:57 +08:00
from .directinput import get_devices as dinput_get_devices
from .directinput import get_controllers as dinput_get_controllers
from .directinput import get_joysticks
2021-04-16 23:21:06 +08:00
try:
from .wintab import get_tablets
except:
pass
2022-10-11 21:53:55 +08:00
from .xinput import get_devices as xinput_get_devices
from .xinput import get_controllers as xinput_get_controllers
from .xinput import XInputControllerManager as ControllerManager
def get_devices(display=None):
return xinput_get_devices() + dinput_get_devices(display)
def get_controllers(display=None):
return xinput_get_controllers() + dinput_get_controllers(display)
2021-04-16 23:21:06 +08:00
elif compat_platform == 'darwin':
from .darwin_hid import get_devices
from .darwin_hid import get_joysticks
from .darwin_hid import get_apple_remote
2022-03-05 23:10:18 +08:00
from .darwin_hid import get_controllers
2022-05-25 09:16:38 +08:00
from .darwin_hid import DarwinControllerManager as ControllerManager