xnano.events
xnano.events
¶
xnano.events
The events a canvas delivers to its hooks: keys, the mouse and everything else.
A hook receives them as context.event; see Context. Build one
with the class methods of Event and deliver it with
Canvas.push to try a hook without a terminal.
Attributes:
-
Event–Something that happened: a key, the mouse, a paste, a resize, a timer or an event of
-
KeyEvent–The details of a key press, repeat or release: the key, the text it types and the
-
MouseEvent–The details of a mouse press, release, drag, move or scroll: the cell, the button and
Event
module-attribute
¶
Event = _CoreEvent
Something that happened: a key, the mouse, a paste, a resize, a timer or an event of your own.
Build one with its class methods to test a hook without a terminal, and read it as
context.event inside the hook.
import xnano
from xnano.events import Event
with xnano.new(width=20, height=1) as canvas:
pressed: list[str] = []
@canvas.hook(on='key')
def remember(context: xnano.Context) -> None:
pressed.append(context.event.key.key)
canvas.push(Event.key_press('a'))
canvas.clock.step(delta_seconds=0.1)
assert pressed == ['a']
KeyEvent
module-attribute
¶
KeyEvent = _CoreKeyEvent
The details of a key press, repeat or release: the key, the text it types and the modifiers held.
Read it from Event.key when a hook needs to know which key was pressed.
import xnano
from xnano.events import Event, KeyEvent
with xnano.new(width=20, height=1) as canvas:
heard: list[KeyEvent] = []
@canvas.hook(on='key')
def remember(context: xnano.Context) -> None:
heard.append(context.event.key)
canvas.push(Event.key_press('ctrl+s'))
canvas.clock.step(delta_seconds=0.1)
assert heard[0].key == 's'
assert heard[0].matches('ctrl+s')
MouseEvent
module-attribute
¶
MouseEvent = _CoreMouseEvent
The details of a mouse press, release, drag, move or scroll: the cell, the button and the wheel steps.
Read it from Event.mouse when a hook needs to know where the pointer is.
import xnano
from xnano.events import Event, MouseEvent
with xnano.new(width=20, height=4) as canvas:
heard: list[MouseEvent] = []
@canvas.hook(on='mouse')
def remember(context: xnano.Context) -> None:
heard.append(context.event.mouse)
canvas.push(Event.mouse_event(column=4, row=2, kind='press', button='left'))
canvas.clock.step(delta_seconds=0.1)
assert (heard[0].column, heard[0].row) == (4, 2)
assert heard[0].button == 'left'
__getattr__
¶
Import an object from xnano_core the first time it is read, then keep it.
Source code in xnano/events.py
def __getattr__(name: str) -> object:
"""Import an object from ``xnano_core`` the first time it is read, then keep it."""
core_name = _CORE_NAMES.get(name)
if core_name is None:
raise AttributeError(
f'xnano.events has no name {name!r}. Its names are {", ".join(__all__)}.'
)
import xnano_core
value = getattr(xnano_core, core_name)
globals()[name] = value
return value