Skip to content

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)

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.

kind property

kind: EventKind

What the event reports.

timestamp_seconds property

timestamp_seconds: float

Seconds on the runtime clock when the event happened.

key property

key: KeyEvent | None

mouse property

mouse: MouseEvent | None

The mouse details of a mouse event, otherwise None.

pasted_text property

pasted_text: str | None

The pasted text of a paste event, otherwise None.

size property

size: Size | None

The new size of a resize event, otherwise None.

timer_key property

timer_key: str | None

The key of a timer event, otherwise None.

custom_name property

custom_name: str | None

The name of a custom event, otherwise None.

custom_data property

custom_data: str | None

The data of a custom event, otherwise None.

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 –

    binding is not a valid key or kind is 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
@classmethod
def key_press(cls, binding: str, *, kind: KeyEventKind = 'press') -> Event:
    """
    A key event for a key name with optional ``+``-joined modifiers.

    Args:
        binding: The key, such as ``'q'``, ``'enter'`` or ``'ctrl+c'``.
        kind: Whether the key is pressed, repeated or released.

    Raises:
        ValueError: ``binding`` is not a valid key or ``kind`` is unknown.

    Examples:
        ```python
        event = Event.key_press('alt+enter')
        assert event.key.key == 'enter'
        assert event.key.modifiers == {'alt'}
        ```
    """

paste classmethod

paste(text: str) -> Event

A paste event.

Examples:

assert Event.paste('hello').pasted_text == 'hello'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
@classmethod
def paste(cls, text: str) -> Event:
    """
    A paste event.

    Examples:
        ```python
        assert Event.paste('hello').pasted_text == 'hello'
        ```
    """

resize classmethod

resize(*, width: int, height: int) -> Event

A resize event.

Examples:

assert Event.resize(width=3, height=2).size.height == 2
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
@classmethod
def resize(cls, *, width: int, height: int) -> Event:
    """
    A resize event.

    Examples:
        ```python
        assert Event.resize(width=3, height=2).size.height == 2
        ```
    """

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
@classmethod
def mouse_event(
    cls,
    *,
    column: int,
    row: int,
    kind: MouseEventKind,
    button: MouseButton | None = None,
    scroll_x: int = 0,
    scroll_y: int = 0,
) -> Event:
    """
    A mouse event.

    Args:
        column: The column under the pointer.
        row: The row under the pointer.
        kind: What the mouse did.
        button: The button involved in a press, release or drag.
        scroll_x: Horizontal scroll steps, negative for left and positive for right.
        scroll_y: Vertical scroll steps, negative for up and positive for down.

    Examples:
        ```python
        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)
        ```
    """

tick classmethod

tick() -> Event

Create a tick event, as a runtime with a tick interval reports.

Examples:

assert Event.tick().kind == 'tick'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
@classmethod
def tick(cls) -> Event:
    """
    Create a tick event, as a runtime with a tick interval reports.

    Examples:
        ```python
        assert Event.tick().kind == 'tick'
        ```
    """

timer classmethod

timer(key: str) -> Event

Create a timer event for the timer called key.

Examples:

assert Event.timer('blink').timer_key == 'blink'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
@classmethod
def timer(cls, key: str) -> Event:
    """
    Create a timer event for the timer called ``key``.

    Examples:
        ```python
        assert Event.timer('blink').timer_key == 'blink'
        ```
    """

wake classmethod

wake() -> Event

Create a wake event, which only makes a runtime run an iteration.

Examples:

assert Event.wake().kind == 'wake'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
@classmethod
def wake(cls) -> Event:
    """
    Create a wake event, which only makes a runtime run an iteration.

    Examples:
        ```python
        assert Event.wake().kind == 'wake'
        ```
    """

exit_requested classmethod

exit_requested() -> Event

Create the event a runtime reports when the application is asked to exit.

Examples:

assert Event.exit_requested().kind == 'exit_requested'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
@classmethod
def exit_requested(cls) -> Event:
    """
    Create the event a runtime reports when the application is asked to exit.

    Examples:
        ```python
        assert Event.exit_requested().kind == 'exit_requested'
        ```
    """

custom classmethod

custom(name: str, *, data: str | None = None) -> Event

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:

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
@classmethod
def custom(cls, name: str, *, data: str | None = None) -> Event:
    """
    Create an event your application defines.

    Inject it with [`Runtime.inject`][xnano_core._xnano_core.Runtime.inject] and listen
    for it with a hook on ``'custom'`` events. Text such as JSON can travel in ``data``.

    Args:
        name: What the event is, such as ``'volume_changed'``.
        data: Optional text that travels with the event.

    Raises:
        ValueError: The name is empty.

    Examples:
        ```python
        event = Event.custom('volume_changed', data='40')
        assert event.kind == 'custom'
        assert (event.custom_name, event.custom_data) == ('volume_changed', '40')
        ```
    """

focus_gained classmethod

focus_gained() -> Event

An event reporting that the terminal window gained focus.

Examples:

assert Event.focus_gained().kind == 'focus_gained'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
@classmethod
def focus_gained(cls) -> Event:
    """
    An event reporting that the terminal window gained focus.

    Examples:
        ```python
        assert Event.focus_gained().kind == 'focus_gained'
        ```
    """

focus_lost classmethod

focus_lost() -> Event

An event reporting that the terminal window lost focus.

Examples:

assert Event.focus_lost().kind == 'focus_lost'
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
@classmethod
def focus_lost(cls) -> Event:
    """
    An event reporting that the terminal window lost focus.

    Examples:
        ```python
        assert Event.focus_lost().kind == 'focus_lost'
        ```
    """

KeyEvent

A key press, repeat or release.

Get one from Event.key or build an event with Event.key_press.

Attributes:

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.

key property

key: str

The key name, such as 'a', 'enter' or 'page_up'.

text property

text: str | None

The text the key types, or None for control keys and chords.

modifiers property

modifiers: frozenset[KeyModifier]

The modifier keys held.

kind property

Whether the key was pressed, repeated or released.

matches

matches(binding: str | KeyBinding) -> bool

Whether this event is the key and modifiers of binding.

Examples:

key = Event.key_press('shift+tab').key
assert key.matches('shift+tab')
assert not key.matches('tab')
Source code in packages/xnano-core/src/xnano_core/_xnano_core/__init__.pyi
def matches(self, binding: str | KeyBinding) -> bool:
    """
    Whether this event is the key and modifiers of ``binding``.

    Examples:
        ```python
        key = Event.key_press('shift+tab').key
        assert key.matches('shift+tab')
        assert not key.matches('tab')
        ```
    """

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 None for 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'

position property

position: Point

The cell under the pointer.

column property

column: int

The column under the pointer.

row property

row: int

The row under the pointer.

kind property

What the mouse did.

button property

button: MouseButton | None

The button involved, or None for moves and scrolls.

scroll_x property

scroll_x: int

Horizontal scroll steps, negative for left and positive for right.

scroll_y property

scroll_y: int

Vertical scroll steps, negative for up and positive for down.