xnano.input
xnano.input
¶
xnano.input
What a hook reads from the keyboard, the mouse, the scroll wheel and the clipboard, and which keys and mouse buttons are held down right now, for programs that poll instead of listening.
Classes:
-
Input–The keys held down right now.
-
KeyboardInput–The key of the current event, as properties of a hook's context.
-
MouseInput–The mouse button or movement of the current event, as properties of a hook's context.
-
ScrollInput–The mouse wheel steps of the current event, as properties of a hook's context.
-
ClipboardInput–The pasted text of the current event, as a property of a hook's context.
KeyNames
module-attribute
¶
One key or mouse button name, or several that count the same, such as ('a', 'left').
The mouse buttons are called 'mouse_left', 'mouse_right' and 'mouse_middle'; see
HeldMouseButtonName.
Input
¶
The keys held down right now.
A hook hears each key press once. A game wants the opposite: ask every step which keys
are down. Get one from Canvas.input and read it anywhere,
from any thread: inside a hook, in a hook(on='tick') update, or in your own loop.
A terminal that reports key releases (kitty, ghostty and wezterm, when the canvas is
opened with keyboard='release_events') tells exactly when a key is let go. Any other
terminal only sends repeats while a key is held, so a key counts as held for a short time
after its last repeat.
The held mouse buttons are in the same vocabulary, named 'mouse_left',
'mouse_right' and 'mouse_middle', so canvas.input.is_down('mouse_left') asks
whether the left button is held.
Attributes:
-
held_keys(frozenset[str]) –The names of the keys and mouse buttons held down right now.
-
pointer(tuple[int, int] | None) –The
(x, y)cell the pointer was last seen at, withxgrowing to the right andydown, orNonebefore it has moved. It is the cell thatcontext.mouse.positiongives as a point.
Examples:
import xnano
with xnano.new(width=20, height=3) as canvas:
hero = canvas.write('@')
@canvas.hook(on='tick', every=1 / 30)
def walk(context: xnano.Context) -> None:
step_x, step_y = canvas.input.direction()
hero.x += step_x
hero.y += step_y
canvas.step()
Create the canvas's input. Use canvas.input instead of calling this.
Parameters:
Methods:
-
is_down–Whether any of the keys or mouse buttons is held down.
-
axis–-1,0or1from a pair of keys: the negative one, neither or both, the -
direction–The way the arrow keys or
wasdpoint, as(step_x, step_y).
Source code in xnano/input.py
held_keys
property
¶
The names of the keys and mouse buttons held down right now.
pointer
property
¶
The (x, y) cell the pointer was last seen at, or None before it moved.
x grows to the right and y down. context.mouse.position gives the same cell
as a point.
is_down
¶
Whether any of the keys or mouse buttons is held down.
Parameters:
-
keys(KeyNames) –One key name such as
'space', or several such as('a', 'left'). A mouse button is'mouse_left','mouse_right'or'mouse_middle'. A key is spelled as inKeyName; a chord such as'ctrl+s'or a modifier alone is not held input.
Raises:
-
TypeError–A name is not text.
-
ValueError–A name is not a key or a mouse button, or is a chord or a modifier.
Source code in xnano/input.py
axis
¶
-1, 0 or 1 from a pair of keys: the negative one, neither or both, the
positive one.
Parameters:
-
negative(KeyNames) –The key or keys that push toward
-1. -
positive(KeyNames) –The key or keys that push toward
1.
Raises:
-
TypeError–A name is not text.
-
ValueError–A name is not a key or a mouse button, or is a chord or a modifier.
Source code in xnano/input.py
direction
¶
direction(*, left: KeyNames = ('a', 'left'), right: KeyNames = ('d', 'right'), up: KeyNames = ('w', 'up'), down: KeyNames = ('s', 'down')) -> tuple[int, int]
The way the arrow keys or wasd point, as (step_x, step_y).
Each step is -1, 0 or 1; y grows downward, like the screen.
Parameters:
-
left(KeyNames, default:('a', 'left')) –The key or keys that mean left.
-
right(KeyNames, default:('d', 'right')) –The key or keys that mean right.
-
up(KeyNames, default:('w', 'up')) –The key or keys that mean up.
-
down(KeyNames, default:('s', 'down')) –The key or keys that mean down.
Raises:
-
TypeError–A name is not text.
-
ValueError–A name is not a key or a mouse button, or is a chord or a modifier.
Source code in xnano/input.py
KeyboardInput
¶
The key of the current event, as properties of a hook's context.
Get one from context.keyboard. It is always there: key is None unless the event
the hook is handling is a key. push delivers a key to the canvas, so a hook can press a
key for the program.
Attributes:
-
key(str | None) –The name of the key, such as
'q'or'enter', orNone. -
kind(Literal['press', 'repeat', 'release'] | None) –'press','repeat'or'release', orNone. -
modifiers(frozenset[str]) –The modifier keys held with it.
-
text(str | None) –The text the key types, or
Nonefor control keys and chords. -
chord(str | None) –The key with its modifiers as one text such as
'ctrl+s', orNone.
Examples:
import xnano
with xnano.new(width=8, height=1) as canvas:
heard = []
@canvas.hook(on='key')
def listen(context: xnano.Context) -> None:
heard.append(context.keyboard.key)
@canvas.hook(on='tick', after=0.1)
def press_for_the_program(context: xnano.Context) -> None:
context.keyboard.push('q')
canvas.step(delta_seconds=0.2)
canvas.deliver_events()
assert heard == ['q']
Create the key view of an event. Use context.keyboard instead of calling this.
Parameters:
-
canvas(Canvas[object, object]) –The canvas the event happened in.
-
event(Event | None) –The event being handled, or
None.
Methods:
-
get–The key event being handled, or
Nonewhen the event is not a key. -
push–Press a key on the canvas, as if it had been typed.
Source code in xnano/input.py
kind
property
¶
kind: Literal['press', 'repeat', 'release'] | None
'press', 'repeat' or 'release', or None for any other event.
modifiers
property
¶
The modifier keys held with the key; empty for any other event.
text
property
¶
text: str | None
The text the key types, or None for control keys, chords and other events.
chord
property
¶
chord: str | None
The key with its modifiers as one text such as 'ctrl+s', or None.
push
¶
Press a key on the canvas, as if it had been typed.
The canvas's hooks hear it at its next step, whatever the input gates say.
Parameters:
Raises:
-
TypeError–valueis neither text nor a key event. -
ValueError–valueis text that does not name a key. The message suggests the closest key names.
Source code in xnano/input.py
MouseInput
¶
The mouse button or movement of the current event, as properties of a hook's context.
Get one from context.mouse. It is always there: position is None unless the
event the hook is handling is a mouse press, release, drag or move. A mouse wheel report is
not a mouse event here; ScrollInput owns it.
Attributes:
-
position(Point | None) –The cell of the event, or
None. -
button(MouseButton | None) –'left','right'or'middle', orNone. -
kind(Literal['press', 'release', 'drag', 'move'] | None) –'press','release','drag'or'move', orNone. -
is_down(bool) –Whether any mouse button is held right now.
Create the mouse view of an event. Use context.mouse instead of calling this.
Parameters:
-
canvas(Canvas[object, object]) –The canvas the event happened in.
-
event(Event | None) –The event being handled, or
None.
Methods:
-
get–The mouse event being handled, or
Nonefor any other event and for the wheel. -
push–Move or click the mouse on the canvas, as if the person had.
Source code in xnano/input.py
kind
property
¶
kind: Literal['press', 'release', 'drag', 'move'] | None
'press', 'release', 'drag' or 'move', or None.
get
¶
get() -> MouseEvent | None
The mouse event being handled, or None for any other event and for the wheel.
Source code in xnano/input.py
push
¶
push(value: MouseEvent | tuple[int, int]) -> None
Move or click the mouse on the canvas, as if the person had.
Parameters:
-
value(MouseEvent | tuple[int, int]) –A
MouseEventof kind press, release, drag or move (or anEventthat holds one), or an(x, y)pair, which moves the pointer to that cell. Acontext.scroll.push((x, y))that follows turns the wheel at that cell.
Raises:
-
TypeError–The value is a mouse wheel event, which belongs to
context.scroll.push, or a pair that is not two whole numbers. -
ValueError–A number of the pair is outside the range of a cell number.
Source code in xnano/input.py
ScrollInput
¶
The mouse wheel steps of the current event, as properties of a hook's context.
Get one from context.scroll. x and y are signed steps that grow right and down,
so scrolling up is y == -1; both are 0 unless the event is a wheel report.
Attributes:
Create the wheel view of an event. Use context.scroll instead of calling this.
Parameters:
-
canvas(Canvas[object, object]) –The canvas the event happened in.
-
event(Event | None) –The event being handled, or
None.
Methods:
-
get–The wheel event being handled, or
Nonefor any other event. -
push–Turn the mouse wheel on the canvas, as if the person had.
Source code in xnano/input.py
get
¶
get() -> MouseEvent | None
The wheel event being handled, or None for any other event.
Source code in xnano/input.py
push
¶
push(value: tuple[int, int] | MouseEvent) -> None
Turn the mouse wheel on the canvas, as if the person had.
Parameters:
-
value(tuple[int, int] | MouseEvent) –The
(x, y)steps to scroll by, at the cell the pointer was last seen at (a pointer moved withcontext.mouse.pushcounts as seen there at once), or aMouseEventof kind scroll.
Raises:
-
TypeError–The value is a mouse event that is not a wheel report, which belongs to
context.mouse.push, or a pair that is not two whole numbers. -
ValueError–A number of the pair is outside the range of a step count.
Source code in xnano/input.py
ClipboardInput
¶
The pasted text of the current event, as a property of a hook's context.
Get one from context.clipboard. text is None unless the event is a paste.
Attributes:
Create the paste view of an event. Use context.clipboard instead of calling this.
Parameters:
-
canvas(Canvas[object, object]) –The canvas the event happened in.
-
event(Event | None) –The event being handled, or
None.
Methods:
-
get–The text that was pasted, or
Nonewhen the event is not a paste. -
push–Paste text on the canvas, as if the person had.
Source code in xnano/input.py
push
¶
push(value: str) -> None
Paste text on the canvas, as if the person had.
Parameters:
-
value(str) –The text to paste.
Raises:
-
TypeError–The value is not text.