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.
Classes:
-
Event–Something that happened: input, a resize, a timer or a frame.
-
KeyEvent–A key press, repeat or release.
-
MouseEvent–A mouse press, release, drag, move or scroll.
Event
¶
Something that happened: input, a resize, a timer or a frame.
Hooks and the runtime report everything as an event. Build events with the class methods to test code without a terminal.
Attributes:
-
kind(EventKind) –What the event reports.
-
timestamp_seconds(float) –Seconds on the runtime clock when the event happened.
-
key(KeyEvent | None) –The key details of a key event, otherwise
None. -
mouse(MouseEvent | None) –The mouse details of a mouse event, otherwise
None. -
pasted_text(str | None) –The pasted text of a paste event, otherwise
None. -
size(Size | None) –The new size of a resize event, otherwise
None. -
timer_key(str | None) –The key of a timer event, otherwise
None. -
custom_name(str | None) –The name of a custom event, otherwise
None. -
custom_data(str | None) –The data of a custom event, otherwise
None.
Examples:
event = Event.key_press('q')
assert event.kind == 'key'
assert event.key.key == 'q'
assert Event.resize(width=80, height=24).size == Size(80, 24)
- API Reference
Methods:
-
key_press–A key event for a key name with optional
+-joined modifiers. -
paste–A paste event.
-
resize–A resize event.
-
mouse_event–A mouse event.
-
tick–Create a tick event, as a runtime with a tick interval reports.
-
timer–Create a timer event for the timer called
key. -
wake–Create a wake event, which only makes a runtime run an iteration.
-
exit_requested–Create the event a runtime reports when the application is asked to exit.
-
custom–Create an event your application defines.
-
focus_gained–An event reporting that the terminal window gained focus.
-
focus_lost–An event reporting that the terminal window lost focus.
timestamp_seconds
property
¶
timestamp_seconds: float
Seconds on the runtime clock when the event happened.
key_press
classmethod
¶
key_press(binding: str, *, kind: KeyEventKind = 'press') -> Event
A key event for a key name with optional +-joined modifiers.
Parameters:
-
binding(str) –The key, such as
'q','enter'or'ctrl+c'. -
kind(KeyEventKind, default:'press') –Whether the key is pressed, repeated or released.
Raises:
-
ValueError–bindingis not a valid key orkindis unknown.
Examples:
event = Event.key_press('alt+enter')
assert event.key.key == 'enter'
assert event.key.modifiers == {'alt'}
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
paste
classmethod
¶
A paste event.
Examples:
resize
classmethod
¶
A resize event.
Examples:
mouse_event
classmethod
¶
mouse_event(*, column: int, row: int, kind: MouseEventKind, button: MouseButton | None = None, scroll_x: int = 0, scroll_y: int = 0) -> Event
A mouse event.
Parameters:
-
column(int) –The column under the pointer.
-
row(int) –The row under the pointer.
-
kind(MouseEventKind) –What the mouse did.
-
button(MouseButton | None, default:None) –The button involved in a press, release or drag.
-
scroll_x(int, default:0) –Horizontal scroll steps, negative for left and positive for right.
-
scroll_y(int, default:0) –Vertical scroll steps, negative for up and positive for down.
Examples:
event = Event.mouse_event(column=0, row=0, kind='scroll', scroll_y=-1)
assert event.mouse.scroll_y == -1
sideways = Event.mouse_event(column=0, row=0, kind='scroll', scroll_x=2)
assert (sideways.mouse.scroll_x, sideways.mouse.scroll_y) == (2, 0)
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
tick
classmethod
¶
tick() -> Event
Create a tick event, as a runtime with a tick interval reports.
Examples:
timer
classmethod
¶
Create a timer event for the timer called key.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
wake
classmethod
¶
wake() -> Event
Create a wake event, which only makes a runtime run an iteration.
Examples:
exit_requested
classmethod
¶
exit_requested() -> Event
Create the event a runtime reports when the application is asked to exit.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
custom
classmethod
¶
Create an event your application defines.
Inject it with Runtime.inject and listen
for it with a hook on 'custom' events. Text such as JSON can travel in data.
Parameters:
-
name(str) –What the event is, such as
'volume_changed'. -
data(str | None, default:None) –Optional text that travels with the event.
Raises:
-
ValueError–The name is empty.
Examples:
event = Event.custom('volume_changed', data='40')
assert event.kind == 'custom'
assert (event.custom_name, event.custom_data) == ('volume_changed', '40')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
focus_gained
classmethod
¶
focus_gained() -> Event
An event reporting that the terminal window gained focus.
Examples:
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
KeyEvent
¶
A key press, repeat or release.
Get one from Event.key or build an event with
Event.key_press.
Attributes:
-
key(str) –The key name, such as
'a','enter'or'page_up'. -
text(str | None) –The text the key types, or
Nonefor control keys and chords. -
modifiers(frozenset[KeyModifier]) –The modifier keys held.
-
kind(KeyEventKind) –Whether the key was pressed, repeated or released.
Examples:
key = Event.key_press('ctrl+c').key
assert key.key == 'c'
assert key.modifiers == {'control'}
assert key.matches('ctrl+c')
Methods:
-
matches–Whether this event is the key and modifiers of
binding.
matches
¶
matches(binding: str | KeyBinding) -> bool
MouseEvent
¶
A mouse press, release, drag, move or scroll.
Attributes:
-
position(Point) –The cell under the pointer.
-
column(int) –The column under the pointer.
-
row(int) –The row under the pointer.
-
kind(MouseEventKind) –What the mouse did.
-
button(MouseButton | None) –The button involved, or
Nonefor moves and scrolls. -
scroll_x(int) –Horizontal scroll steps, negative for left and positive for right.
-
scroll_y(int) –Vertical scroll steps, negative for up and positive for down.
Examples:
mouse = Event.mouse_event(column=4, row=2, kind='press', button='left').mouse
assert (mouse.column, mouse.row) == (4, 2)
assert mouse.button == 'left'