handler—group and handle events

class EventHandler(scheduler)[source]

Bases: object

Handles events.

Parameters:schedulersched.Scheduler instance to use for determining the current framerate.

You probably want to call normalise(), then call update() 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 a dict of named events (only supports getting, setting and deleting items).
  • The 'domain' name is reserved.
  • The __contains__ method (event in event_handler) works for BaseEvent instances as well as names.
scheduler = None

As passed to the constructor.

active_domains = None

A set of domains that will receive relevant events.

inactive_domains = None

A set of domains that have been disabled through disable().

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 an Event 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 and f2 for keydown events, and f3 and f4 for both keydown and keyup 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])
update()[source]

Process Pygame events and call callbacks.

domains(*domains) → evts[source]

Get a set of all events in the given domains.

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.

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.

save(name, *domains)[source]

Not implemented.

save_s(*domains)[source]

Not implemented.

unload(*domains) → evts[source]

Remove all events in the given domains.

Returns:all removed events as (unnamed, named), like domains().

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 and Input.device_id for details (including possible device ID values).

grab(cb, *types)[source]

Not implemented.

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.

monitor_deadzones(*deadzones)[source]

Not implemented.

stop_monitor_deadzones()[source]

Not implemented.

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 the deadzone attribute of each matching input to deadzone. 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 also Input.device_id.

An item may also be just (device, deadzone).