xnano.events
xnano.events
¶
xnano.events
Inspect keyboard, mouse, resize, focus, and clipboard events.
Classes:
-
AbstractEventData–Base for event payloads.
-
KeyboardEventData–Keyboard input and its normalized binding.
-
MouseEventData–Mouse input at a cell coordinate.
-
ResizeEventData–New viewport dimensions.
-
ClipboardEventData–Text received from paste or clipboard input.
-
FocusEventData–Terminal or field focus transition.
-
TickEventData–Elapsed time reported by the runtime clock.
-
Event–Unified event produced by native input, web input, or an action.
Functions:
-
normalize_keyboard_binding–Normalize a keyboard binding for comparison.
-
parse_binding_tuple–Split a binding into modifiers and its primary key.
-
event_from_core–Convert a native core event into the public
Eventshape.
Attributes:
-
EventDataType(TypeAlias) –Public event payload category.
-
KeyboardEventKind(TypeAlias) –Keyboard transition reported by a runtime.
-
MouseEventKind(TypeAlias) –Mouse movement, button, or wheel transition.
-
FocusEventKind(TypeAlias) –Terminal or field focus transition.
-
EventData(TypeAlias) –Payload carried by a public
Event.
EventDataType
module-attribute
¶
EventDataType: TypeAlias = Literal[
"keyboard",
"mouse",
"resize",
"clipboard",
"focus",
"tick",
"other",
]
Public event payload category.
KeyboardEventKind
module-attribute
¶
Keyboard transition reported by a runtime.
MouseEventKind
module-attribute
¶
MouseEventKind: TypeAlias = Literal[
"press",
"release",
"drag",
"move",
"scroll_up",
"scroll_down",
"scroll_left",
"scroll_right",
]
Mouse movement, button, or wheel transition.
FocusEventKind
module-attribute
¶
Terminal or field focus transition.
EventData
module-attribute
¶
EventData: TypeAlias = (
AbstractEventData
| KeyboardEventData
| MouseEventData
| ResizeEventData
| ClipboardEventData
| FocusEventData
| TickEventData
)
Payload carried by a public Event.
AbstractEventData
dataclass
¶
KeyboardEventData
dataclass
¶
KeyboardEventData(
_native_event: object | None = None,
_kind: KeyboardEventKind = "press",
_binding: str | None = None,
_character: str | None = None,
)
Bases: AbstractEventData
Keyboard input and its normalized binding.
Attributes:
-
type(Literal['keyboard']) –Payload category, always
"keyboard". -
binding(KeyboardBinding) –Normalized binding such as
"ctrl+s". -
key(KeyboardKey | str | None) –Primary key.
-
kind(KeyboardEventKind) –Keyboard transition.
-
modifiers(list[KeyboardModifier]) –Modifier keys held during the event.
-
character(str | None) –Typed character, when available.
Examples:
Methods:
-
from_binding–Create keyboard data from a binding string.
-
matches–Return whether this event matches any binding.
from_binding
classmethod
¶
from_binding(
binding: str,
*,
kind: KeyboardEventKind = "press",
character: str | None = None
) -> "KeyboardEventData"
Create keyboard data from a binding string.
Parameters:
-
binding(str) –Keyboard binding to synthesize.
-
kind(KeyboardEventKind, default:'press') –Keyboard transition.
-
character(str | None, default:None) –Text character, when the binding represents one.
Returns:
-
'KeyboardEventData'–Synthetic keyboard event data.
Source code in xnano/events.py
matches
¶
matches(*bindings: KeyboardBinding) -> bool
Return whether this event matches any binding.
Bare modifiers ("shift", "ctrl", "alt") match a
modifier-only key press, not a modified key such as "shift+a".
Parameters:
-
*bindings(KeyboardBinding, default:()) –Candidate keyboard bindings.
Returns:
-
bool–Whether at least one binding matches.
Source code in xnano/events.py
MouseEventData
dataclass
¶
MouseEventData(
kind: MouseEventKind,
x: int,
y: int,
button: MouseButton = "unknown",
field: str | None = None,
group: str | None = None,
)
Bases: AbstractEventData
Mouse input at a cell coordinate.
Attributes:
-
kind(MouseEventKind) –Mouse transition.
-
x(int) –Zero-based cell column.
-
y(int) –Zero-based cell row.
-
button(MouseButton) –Button involved in the transition.
-
field(str | None) –Field under the pointer for synthetic or web input.
-
group(str | None) –Field group under the pointer.
Examples:
button
class-attribute
instance-attribute
¶
button: MouseButton = 'unknown'
Button involved in the transition.
ResizeEventData
dataclass
¶
FocusEventData
dataclass
¶
FocusEventData(
kind: FocusEventKind,
field: str | None = None,
group: str | None = None,
)
Bases: AbstractEventData
Terminal or field focus transition.
Attributes:
-
kind(FocusEventKind) –Focus transition.
-
field(str | None) –Field name for field focus.
-
group(str | None) –Shared focus group for field focus.
TickEventData
dataclass
¶
TickEventData(elapsed_ms: int = 0)
Bases: AbstractEventData
Elapsed time reported by the runtime clock.
Attributes:
-
elapsed_ms(int) –Milliseconds since the previous tick.
Event
dataclass
¶
Unified event produced by native input, web input, or an action.
Attributes:
-
data(EventData) –Public event payload.
-
type(EventDataType) –Payload category.
-
keyboard_event(KeyboardEventData | None) –Keyboard payload, when present.
-
mouse_event(MouseEventData | None) –Mouse payload, when present.
-
resize_event(ResizeEventData | None) –Resize payload, when present.
-
clipboard_event(ClipboardEventData | None) –Clipboard payload, when present.
-
focus_event(FocusEventData | None) –Focus payload, when present.
-
tick_event(TickEventData | None) –Tick payload, when present.
Examples:
event = Event.from_data(KeyboardEventData.from_binding("enter"))
if event.keyboard_event and event.keyboard_event.matches("enter"):
submit()
Methods:
-
from_data–Create an event from a synthetic payload.
-
is_clipboard_event–Return whether this is clipboard input.
-
is_focus_event–Return whether this is a focus transition.
-
is_keyboard_event–Return whether this is keyboard input.
-
is_mouse_event–Return whether this is mouse input.
-
is_resize_event–Return whether this is a viewport resize.
-
is_tick_event–Return whether this is a runtime clock tick.
keyboard_event_kind
property
¶
keyboard_event_kind: KeyboardEventKind | None
Keyboard event kind when this is a keyboard event.
keyboard_modifiers
property
¶
keyboard_modifiers: list[KeyboardModifier]
Modifier keys when this is a keyboard event.
mouse_event_kind
property
¶
mouse_event_kind: MouseEventKind | None
Mouse event kind when this is a mouse event.
resize_size
property
¶
Viewport size from a resize event, or None.
from_data
classmethod
¶
from_data(data: EventData) -> 'Event'
Create an event from a synthetic payload.
Parameters:
-
data(EventData) –Public event payload.
Returns:
-
'Event'–Event wrapping the payload.
normalize_keyboard_binding
¶
Normalize a keyboard binding for comparison.
Parameters:
-
binding(str) –Binding such as
"ctrl+s".
Returns:
Source code in xnano/events.py
parse_binding_tuple
¶
parse_binding_tuple(
binding: str,
) -> tuple[list[KeyboardModifier], str]
Split a binding into modifiers and its primary key.
Parameters:
-
binding(str) –Binding such as
"shift+tab".
Returns:
-
tuple[list[KeyboardModifier], str]–Modifier names and primary key.
Source code in xnano/events.py
event_from_core
¶
Convert a native core event into the public Event shape.
Browser input and synthetic actions should construct the same public classes; this is the single native conversion path.
Parameters:
-
core_event(CoreEvent) –Event polled from
CoreSession.
Returns:
-
Event–A public
Eventhandlers cannot distinguish from other sources.