Events

Events are the principle way users can attach connect thier own functions, and built in methods event triggers.

mechanica.on_time(method, period[, start][, end][, distribution])

Binds a function or method to a elapsed time interval.

on_time must be called with all keyword arguments, since there are a large number of options this method accepts.

Parameters:
  • method – A Python function or method that will get called. Signature of the method should be method(time), in that it gets a single current time argument.
  • period (float) – Time interval that the event should get fired.
  • start (float) – [optional] Start time for the event.
  • end (float) – [optional] end time for the event.
  • distribution (str) – [optional] String that identifies the statistical distribution for event times. Only supported distibution currently is “exponential”. If there is no distibution argument, the event is called deterministically.
mechanica.on_keypress(callback)

Register for a key press event. The callback function will get called with a key press object every time the user presses a key. The Keypress object has a key_name property to get the key value:

def my_keypress_handler(e):
    print("key pressed: ", e.key_name)

m.on_keypress(my_keypress_handler)