handler
—group and handle events¶
-
class
EventHandler
(scheduler)[source]¶ Bases:
object
Handles events.
Parameters: scheduler – sched.Scheduler
instance to use for determining the current framerate.You probably want to call
normalise()
, then callupdate()
every frame to process and progagate Pygame events and call callbacks.Some notes:
- An event may be placed in a ‘domain’, which is represented by a string name.
- Events are named or unnamed, and an
EventHandler
acts like adict
of named events (only supports getting, setting and deleting items). - The
'domain'
name is reserved. - The
__contains__
method (event in event_handler
) works forBaseEvent
instances as well as names.
-
scheduler
= None¶ As passed to the constructor.
-
active_domains
= None¶ A
set
of domains that will receive relevant events.
-
evts
= None¶ A
set
of all registered unnamed events.
-
inputs
= None¶ All inputs registered with events in this handler.
-
autocentre_mouse
= None¶ Whether to capture the mouse cursor by centring it on the window every frame. You might also want to grab all input (
pygame.event.set_grab
).
-
add
(*evts, **named_evts) → unnamed[source]¶ Register events.
Arguments are any number of events. Keyword arguments define named events with the key as the name. An event can be a
BaseEvent
instance, or a sequence of Pygame event IDs and functions to create anEvent
that listens for the given Pygame events and has the functions as callbacks. For example,handler.add( (pygame.KEYDOWN, f1, f2), (f3, pygame.KEYDOWN, f4, pygame.KEYUP) )
will register callbacks
f1
andf2
forkeydown
events, andf3
andf4
for bothkeydown
andkeyup
events.Returns: a list of added unnamed events (positional arguments) (possibly created in this call).
-
rm
(*evts)[source]¶ Takes any number of registered event names or events to remove them.
Raises
KeyError
if any arguments are missing.
-
cb
(pos_cbs={}, **kw_cbs)[source]¶ Attach callbacks to named events.
Each dict has keys as event names and values as callback functions or sequences of callback functions. For example:
evthandler.cb({'jump': jump}, walk=[e.walk for e in entities])
-
load
(filename, domain = None) → evts[source]¶ Load events from a configuration file (see
conffile
).Parameters: - filename – a filename to load as the configuration file, under
conf.EVT_DIR
. - domain – domain to place loaded events in.
Returns: {name: event}
for loaded events.- filename – a filename to load as the configuration file, under
-
load_s
(s, domain = None) → evts[source]¶ Load events from a configuration string (see
conffile
).Parameters: - s – string to parse as an event configuration.
- domain – domain to place loaded events in.
Returns: {name: event}
for loaded events.
-
unload
(*domains) → evts[source]¶ Remove all events in the given domains.
Returns: all removed events as (unnamed, named)
, likedomains()
.Raises
KeyError
if a domain is missing.
-
disable
(*domains)[source]¶ Disable event handling in all of the given domains.
Missing or already disabled domains are ignored (a domain is missing if it is empty).
-
enable
(*domains)[source]¶ Re-enable event handling in all of the given domains.
Missing or already active domains are ignored. Beware that state is preserved, so buttons that were held when disabled remain held when enabled, no matter how much time has passed, without sending a
DOWN
.
-
assign_devices
(**devices)[source]¶ Assign device IDs to inputs by device variable.
Parameters: devices – keyword arguments with the argument name the variable and the value the new device ID for each input with this device variable. See
Input.device_var
andInput.device_id
for details (including possible device ID values).
-
normalise
()[source]¶ Determine and set states of all inputs, where possible.
This includes axis positions, button held states, etc..
You should generally call this whenever you start using this event handler, either for the first time, or after a period of letting something else handle events.
-
set_deadzones
(*deadzones)[source]¶ Set deadzones for all registered inputs that support it.
Attr deadzones: any number of
((device, device_id=True, attrs={}), deadzone)
tuples to set thedeadzone
attribute of each matching input todeadzone
.device_id
may be a variable (Input.device_var
) or non-string ID (Input.device_id
).attrs
is a dict of attributes the input must have. See alsoInput.device_id
.An item may also be just
(device, deadzone)
.