The Event Listener Module¶
The Eventlistener wraps pygames event-loop.
The Core method is the listen() method. It gathers the events that have piled up in pygame so far and processes them acording to handler functions. This allows for a main-loop free script design, which is more suited for experimental paradigms. In a typical experiment the script just waits for a userinput and does nothing, or only a very few things in between. Approaching this need with a main event-loop requires the implementation of some sort of statemachine, which again requires quite some code.
The EventListener enables one to write scripts in a time-linear manner, and only dab into local event-loops whenever neccessary throught the listen-function.
There are a few pre-implemented methods, which cover most of those use-cases in the developement of experimental paradigms.
- wait_for_keypress() will return once a key was pressed n times.
- wait_for_keys_timed_out() will wait for one of multiple possible keys,
- but return after the given timeout in any case
- and wait_for_seconds will simply wait the given time, but in the mean-time run what ever handlers were passed to the EventListener.
By default, there is one permanent handler, which will call exit(1) when Ctrl + c is pressed.
-
class
pyparadigm.eventlistener.
EventConsumerInfo
[source]¶ Can be returned by event-handler functions to communicate with the listener. For Details see EventListener
-
class
pyparadigm.eventlistener.
EventListener
(permanent_handlers=None, use_ctrl_c_handler=True)[source]¶ Parameters: - permanent_handlers (iterable) – iterable of permanent handlers
- use_ctrl_c_handler (Bool) – specifies whether a handler that quits the script when ctrl + c is pressed should be used
-
listen
(temporary_handlers=None)[source]¶ When listen() is called all queued pygame.Events will be passed to all registered listeners. There are two ways to register a listener:
- as a permanent listener, that is always executed for every event. These
- are registered by passing the handler-functions during construction
- as a temporary listener, that will only be executed during the current
- call to listen(). These are registered by passing the handler functions as arguments to listen()
When a handler is called it can provoke three different reactions through its return value.
- It can return EventConsumerInfo.DONT_CARE in which case the EventListener
- will pass the event to the next handler in line, or go to the next event, if the last handler was called.
- It can return EventConsumerInfo.CONSUMED in which case the event will not
- be passed to following handlers, and the next event in line will be processed.
- It can return anything else (including None, which will be returned if no
- return value is specified) in this case the listen()-method will return the result of the handler.
Therefore all permanent handlers should usually return EventConsumerInfo.DONT_CARE
-
wait_for_keys
(*keys, timeout=0)[source]¶ Waits until one of the specified keys was pressed, and returns which key was pressed.
Parameters: - keys (iterable) – iterable of integers of pygame-keycodes, or simply multiple keys passed via multiple arguments
- timeout (float) – number of seconds to wait till the function returns
Returns: The keycode of the pressed key, or None in case of timeout
Return type: int