inputs
—filtered subsets of Pygame events¶
-
mod_devices
= {'evt': ('evt',), 'kbd': ('kbd',), 'mouse': ('kbd', 'mouse'), 'pad': ('pad',)}¶ {device: allowed_mod_devices}
forButtonInput
instances. An input fordevice
device
may only have modifiers withdevice
inallowed_mod_devices
.
-
device_init_handlers
= {'pad': <function _init_pad>}¶ {device: init_fn}
giving initialisation functions to initialise devices where possible. These functions take theInput.device_id
to initialise for, or no argument to initialise for all devices of this type, and should return a sequence of device IDs that have been successfully initialised.
-
class
mbtn
[source]¶ Contains mouse button aliases.
-
LEFT
= 1¶
-
MIDDLE
= 2¶
-
RIGHT
= 3¶
-
UP
= 4¶
-
DOWN
= 5¶
-
-
class
Input
(*pgevts)[source]¶ Bases:
object
Base class for handling input events. Does nothing by itself.
Parameters: pgevts – Pygame event IDs to listen for. If a subclass has a
pgevts
attribute, this is a list of events to add to the argument at initialisation.Comparing inputs for equality compares filters only (so inputs of different types may be equal).
-
components
= 0¶ Number of components (‘directions’/’button-likes’) represented by this input.
-
device
= None¶ The string device name that this input type corresponds to (see
inputs_by_name
).
-
provides
= None¶ An
{id: provided}
dict of ‘interfaces’ this input provides, with keys'button'
,'axis'
,'relaxis'
.
-
device_var
= None¶ Variable representing the current device ID; may be a string as a variable name, or
None
. See alsoEventHandler.assign_devices()
).
-
filters
= None¶ A
{pgevt_attr: val}
dict that represents how events are filtered before being passed to this input (seefilter()
).
-
eh
¶ EventHandler
forevt
, orNone
.
-
filter
(attr, *vals, refilter = False) → self[source]¶ Filter events passed to this input.
Parameters: - attr – Pygame event attribute to filter by.
- vals – allowed values of the given attribute for filtered events.
- refilter – if
True
, replace previous filtering byattr
with the givenvals
, else add to the values already filtered by.
-
unfilter
(attr, *vals)[source]¶ Remove filtering by the given attribute.
Parameters: - attr – Pygame event attribute to modify filtering for.
- vals – values to remove filtering for. If none are given, all filtering
by
attr
is removed.
-
device_id
¶ The particular device that this input captures input for.
May be
True
, in which case all such devices work through this input.May be
None
, in which case no input will be registered; this is done by filtering byinvalid_device_id
.Subclasses may set an attribute
device_id_attr
, in which case setting this attribute filters usingdevice_id_attr
as the event attribute and the set value as the attribute value to filter by. If a subclass does not providedevice_id_attr
and does not override the setter, this operation raisesTypeError
.
-
handle
(pgevt)[source]¶ Called by
EventHandler
with apygame.event.Event
.The passed event matches
filters
.Returns: whether anything in the input’s state changed.
-
-
class
BasicInput
(*pgevts)[source]¶ Bases:
engine.evt.inputs.Input
An input that handles raw Pygame events.
Parameters: pgevts – Pygame event IDs to listen for. -
pgevts
= None¶ Pygame event IDs as passed to the constructor.
-
-
class
ButtonInput
([button, ]*mods)[source]¶ Bases:
engine.evt.inputs.Input
Abstract base class representing a button-like action (
Input
subclass).Parameters: - button – button ID to listen for. To use this, subclasses must set a
button_attr
property to filter by that Pygame event attribute with this ID as the value. Otherwise, they must implement filtering themselves. - mods – inputs to use as modifiers. Each may be a
ButtonInput
, a sequence of them, or(input, component)
giving the component of the input to use (from0
toinput.components - 1
).
Subclasses must have a
device
inmod_devices
, which restricts allowed devices of modifiers.-
components
= 1¶
-
is_mod
= None¶ Whether this input is acting as a modifier.
-
used_components
= None¶ {container: components}
for each container (such as anEvent
, or anotherButtonInput
as a modifier).components
is a sequence of the components of this input that the container uses.
The button ID this input represents, as taken by the constructor.
-
held
(container)[source]¶ A list of the held state of this button for each component.
Each item is a bool that corresponds to the component in the same position in
used_components
for the given container.
-
down
(component=0, evt=True)[source]¶ Set the given component’s button state to down.
Parameters: evt – whether to let the containing event know about this.
-
up
(component=0, evt=True)[source]¶ Set the given component’s button state to up.
Parameters: evt – whether to let the containing event know about this.
-
set_held
(held, evts=False, component=0)[source]¶ Set the held state of the button on the given component.
Parameters: evts – whether to trigger button down/up events if the held state changes.
- button – button ID to listen for. To use this, subclasses must set a
-
class
KbdKey
(key, *mods)[source]¶ Bases:
engine.evt.inputs.ButtonInput
Keyboard key.
Parameters: key – the key code (required). -
device
= 'kbd'¶
-
name
= 'key'¶
-
pgevts
= (2, 3)¶
-
down_pgevts
= (2,)¶
-
-
class
MouseButton
(button, *mods)[source]¶ Bases:
engine.evt.inputs.ButtonInput
Mouse button.
The
button
argument is required, and is the mouse button ID.-
device
= 'mouse'¶
-
name
= 'button'¶
-
pgevts
= (5, 6)¶
-
down_pgevts
= (5,)¶
-
-
class
PadButton
(device_id, button, *mods)[source]¶ Bases:
engine.evt.inputs.ButtonInput
Gamepad button.
Parameters: - device_id – the gamepad’s device ID, either a variable
(
device_var
) or a non-string ID (device_id
). - button – as taken by
ButtonInput
. - mods – as taken by
ButtonInput
.
-
device
= 'pad'¶
-
name
= 'button'¶
-
pgevts
= (10, 11)¶
-
device_id_attr
= 'joy'¶
-
down_pgevts
= (10,)¶
- device_id – the gamepad’s device ID, either a variable
(
-
class
AxisInput
([axis][, thresholds], *mods)[source]¶ Bases:
engine.evt.inputs.ButtonInput
Abstract base class representing 2-component axes.
Parameters: - axis – axis ID to listen for. To use this, subclasses must set an
axis_attr
property to filter by that Pygame event attribute with this ID as the value, and with an attribute giving the axis’s value. Otherwise, they must implement filtering themselves. - thresholds –
required if the axis is to act as a button. For each axis (that is, for each pair of
Input.components
), this list has two elements:down
followed byup
, positive numbers giving the magnitude of the value of the axis in either direction that triggers a button down or up event. For example, a 2-component axis might have(.6, .4)
.A subclass with more than 2 components may pass a length-2 sequence here, which is expanded by assuming the same thresholds for each axis.
- mods – as taken by
ButtonInput
. Only used if this axis is treated as a button.
Subclasses must have an even number of components.
-
components
= 2¶
-
axis
= None¶ Axis ID, as passed to the constructor.
-
thresholds
= None¶ As passed to the constructor.
-
pos
¶ Sequence of positions for each axis.
-
deadzone
¶ Axis value magnitude below which the value is mapped to
0
; defaults to0
.Above this value, the mapped value increases linearly from
0
.
-
axis_motion
(axis, apos)[source]¶ Signal a change in axis position.
Parameters: - axis – the index of the axis to modify (a 2-component
AxisInput
has one axis, with index0
). - apos – the new axis position (
-1 <= apos <= 1
).
- axis – the index of the axis to modify (a 2-component
- axis – axis ID to listen for. To use this, subclasses must set an
-
class
PadAxis
(device_id, axis, [thresholds, ]*mods)[source]¶ Bases:
engine.evt.inputs.AxisInput
Gamepad axis.
Parameters: - device_id – the gamepad’s device ID, either a variable
(
device_var
) or a non-string ID (device_id
). - axis – as taken by
AxisInput
. - thresholds – as taken by
AxisInput
. - mods – as taken by
ButtonInput
.
-
device
= 'pad'¶
-
name
= 'axis'¶
-
device_id_attr
= 'joy'¶
-
pgevts
= (7,)¶
-
axis_attr
= 'axis'¶
-
axis_val_attr
= 'value'¶
- device_id – the gamepad’s device ID, either a variable
(
-
class
PadHat
(device_id, axis, [thresholds, ]*mods)[source]¶ Bases:
engine.evt.inputs.AxisInput
Gamepad hat.
Parameters: - device_id – the gamepad’s device ID, either a variable
(
device_var
) or a non-string ID (device_id
). - hat – the hat ID to listen for.
- thresholds – as taken by
AxisInput
. - mods – as taken by
ButtonInput
.
-
components
= 4¶
-
device
= 'pad'¶
-
name
= 'hat'¶
-
device_id_attr
= 'joy'¶
-
pgevts
= (9,)¶
-
axis_attr
= 'hat'¶
-
axis_val_attr
= 'value'¶
- device_id – the gamepad’s device ID, either a variable
(
-
class
RelAxisInput
([relaxis][, bdy][, thresholds], *mods)[source]¶ Bases:
engine.evt.inputs.AxisInput
Abstract base class representing 2-component relative axes.
Parameters: - relaxis – axis ID to listen for. To use this, subclasses must set a
relaxis_attr
property to filter by that Pygame event attribute with this ID as the value, and with an attribute giving the axis’s value. Otherwise, they must implement filtering themselves. - bdy – required if the relative axis is to act as an axis or a button. For each axis (each 2 components), this sequence contains a positive number giving the maximum magnitude of the axis. The normalised axis position is then obtained by dividing by this value. May be a single number instead of a one-item sequence.
- thresholds – as taken by
AxisInput
. Only used if this relative axis is treated as an axis, and required if it is to act as a button. - mods – as taken by
ButtonInput
. Only used if this relative axis is treated as a button.
A relative axis is one where events convey a change in the axis’s value, rather than its absolute position. Subclasses must have an even number of components.
Note that using the same component of an instance of a subclass for two different events (or using the same component twice for a single
MultiEvent
) is not supported, and behaviour in this case is undefined.-
components
= 2¶
-
relaxis
= None¶ Axis ID, as passed to the constructor.
-
bdy
= None¶ As taken by the constructor.
- relaxis – axis ID to listen for. To use this, subclasses must set a
-
class
MouseAxis
([bdy][, thresholds], *mods)[source]¶ Bases:
engine.evt.inputs.RelAxisInput
Represents both mouse axes.
Arguments are as taken by
RelAxisInput
.-
components
= 4¶
-
device
= 'mouse'¶
-
name
= 'axis'¶
-
pgevts
= (4,)¶
-
relaxis_val_attr
= 'rel'¶
-
-
mod
= <engine.evt.inputs._mod object>¶ Contains objects that act as specific keyboard modifiers: CTRL, SHIFT, ALT, META.