Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ _Notes on the upcoming release will go here._

### What's new

#### Extended function keys and modifier-only keys (#129)

{class}`~libvt.input.KeyEvent` and {func}`~libvt.input.encode_input` now accept
`f13` through `f35` (encoding xterm tilde sequences in legacy mode and Kitty
functional `CSI u` codepoints `57376`–`57398` when enhanced) and left/right
modifier-only keys (`shift_left` through `meta_right`, encoding Kitty
`57441`–`57452` under {attr}`~libvt.input.KittyKeyboardFlags.REPORT_ALL_KEYS`
and returning `b""` otherwise).

#### Drive terminal processes synchronously (#142)

{func}`~libvt.contrib.subprocess.open_terminal` opens an interactive child on a
Expand Down
24 changes: 24 additions & 0 deletions docs/topics/protocols-and-input.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,30 @@ b'\x1b[97;;97u'
b'\x1b[97;1:3u'
```

Extended function keys (`f13`–`f35`) emit xterm tilde sequences (`CSI 25 ~`,
`26 ~`, `28 ~`, `29 ~`, `31 ~`–`34 ~` — skipping `27` and `30` — for
`f13`–`f20`, and `CSI 42 ~`–`56 ~` for `f21`–`f35`) in legacy mode, and Kitty
functional `CSI u` codepoints (`57376`–`57398`) when
{attr}`~libvt.input.KittyKeyboardFlags.DISAMBIGUATE`, `REPORT_EVENT_TYPES`, or
`REPORT_ALL_KEYS` is active. Left and right modifier-only keys (`shift_left`,
`ctrl_left`, `alt_left`, `super_left`, `hyper_left`, `meta_left`, and their
`*_right` counterparts) return `b""` unless `REPORT_ALL_KEYS` is active, in
which case they emit Kitty functional `CSI u` codepoints (`57441`–`57452`):

```python
>>> from libvt import Terminal
>>> from libvt.input import KeyAction, KeyEvent
>>> term = Terminal()
>>> term.encode(KeyEvent("f13")), term.encode(KeyEvent("shift_left"))
(b'\x1b[25~', b'')
>>> term.feed(b"\x1b[>10u")
>>> (
... term.encode(KeyEvent("f13")),
... term.encode(KeyEvent("shift_left", action=KeyAction.RELEASE)),
... )
(b'\x1b[57376u', b'\x1b[57441;1:3u')
```

### Mouse coordinates and wheel input

`MouseEvent.row` and `MouseEvent.col` start at zero. Libvt clamps each
Expand Down
66 changes: 62 additions & 4 deletions src/libvt/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,13 @@ class KeyEvent:
(``"enter"``, ``"tab"``, ``"escape"``, ``"backspace"``, ``"up"``,
``"down"``, ``"right"``, ``"left"``, ``"home"``, ``"end"``,
``"page_up"``, ``"page_down"``, ``"insert"``, ``"delete"``,
``"f1"`` through ``"f12"``, and keypad ``"kp_0"`` through ``"kp_9"``,
``"kp_decimal"``, ``"kp_divide"``, ``"kp_multiply"``,
``"kp_subtract"``, ``"kp_add"``, ``"kp_enter"``, ``"kp_equal"``,
``"kp_separator"``).
``"f1"`` through ``"f35"``, modifier-only keys (``"shift_left"``,
``"ctrl_left"``, ``"alt_left"``, ``"super_left"``, ``"hyper_left"``,
``"meta_left"``, ``"shift_right"``, ``"ctrl_right"``, ``"alt_right"``,
``"super_right"``, ``"hyper_right"``, ``"meta_right"``), and keypad
``"kp_0"`` through ``"kp_9"``, ``"kp_decimal"``, ``"kp_divide"``,
``"kp_multiply"``, ``"kp_subtract"``, ``"kp_add"``, ``"kp_enter"``,
``"kp_equal"``, ``"kp_separator"``).
modifiers : KeyMod
Active modifier bitmask.
action : KeyAction
Expand Down Expand Up @@ -368,6 +371,34 @@ class PasteEvent:
"f12": 24,
}

# (legacy_tilde_num, kitty_functional_cp)
_EXT_FKEYS: dict[str, tuple[int, int]] = {
"f13": (25, 57376),
"f14": (26, 57377),
"f15": (28, 57378),
"f16": (29, 57379),
"f17": (31, 57380),
"f18": (32, 57381),
"f19": (33, 57382),
"f20": (34, 57383),
**{f"f{n}": (42 + (n - 21), 57376 + (n - 13)) for n in range(21, 36)},
}

_MODIFIER_KEY_CP: dict[str, int] = {
"shift_left": 57441,
"ctrl_left": 57442,
"alt_left": 57443,
"super_left": 57444,
"hyper_left": 57445,
"meta_left": 57446,
"shift_right": 57447,
"ctrl_right": 57448,
"alt_right": 57449,
"super_right": 57450,
"hyper_right": 57451,
"meta_right": 57452,
}

_SS3_FKEYS: dict[str, str] = {
"f1": "P",
"f2": "Q",
Expand Down Expand Up @@ -813,6 +844,33 @@ def _encode_key(
final="~",
)

# Extended function keys (F13-F35): Kitty CSI u when enhanced, xterm CSI ~ legacy.
if k_low in _EXT_FKEYS:
legacy_num, kitty_cp = _EXT_FKEYS[k_low]
if disambig or report_events:
assoc = (
event.text
if (report_assoc and event.action != KeyAction.RELEASE)
else ""
)
return _format_kitty_csi(
kitty_cp,
mod_param=mod_param,
ev_suffix=ev_suffix,
assoc_text=assoc,
)
return _format_kitty_csi(legacy_num, mod_param=mod_param, final="~")

# Modifier-only keys emit CSI u only under Kitty REPORT_ALL_KEYS.
if k_low in _MODIFIER_KEY_CP:
if not report_all:
return b""
return _format_kitty_csi(
_MODIFIER_KEY_CP[k_low],
mod_param=mod_param,
ev_suffix=ev_suffix,
)

use_xtmod = kitty_flags == KittyKeyboardFlags.NONE and modify_other_keys in (1, 2)

# Keypad keys (DECNKM ?66 / ESC = / ESC >)
Expand Down
155 changes: 155 additions & 0 deletions tests/test_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,3 +639,158 @@ def test_kitty_report_alternate_keys_and_associated_text() -> None:

with pytest.raises(ValueError, match="single character"):
KeyEvent(key="a", shifted_key="ab")


def test_extended_function_keys_and_modifier_only_keys() -> None:
"""Encode ``f13``-``f35`` and left/right modifier keys in Kitty and legacy modes."""
term = libvt.Terminal(rows=4, cols=20)

expected_legacy = {
"f13": 25,
"f14": 26,
"f15": 28,
"f16": 29,
"f17": 31,
"f18": 32,
"f19": 33,
"f20": 34,
**{f"f{n}": 42 + (n - 21) for n in range(21, 36)},
}
expected_kitty_fkeys = {f"f{n}": 57376 + (n - 13) for n in range(13, 36)}
expected_modifiers = {
"shift_left": 57441,
"ctrl_left": 57442,
"alt_left": 57443,
"super_left": 57444,
"hyper_left": 57445,
"meta_left": 57446,
"shift_right": 57447,
"ctrl_right": 57448,
"alt_right": 57449,
"super_right": 57450,
"hyper_right": 57451,
"meta_right": 57452,
}

# Legacy mode: F13-F35 emit xterm tilde CSI; modifiers emit b"".
for name, tilde in expected_legacy.items():
assert term.encode(KeyEvent(name)) == f"\x1b[{tilde}~".encode("ascii")
assert term.encode(KeyEvent.parse(f"ctrl+{name}")) == f"\x1b[{tilde};5~".encode(
"ascii"
)
assert term.encode(KeyEvent(name, action=KeyAction.RELEASE)) == b""

for name in expected_modifiers:
assert term.encode(KeyEvent(name)) == b""
assert term.encode(KeyEvent.parse(f"ctrl+{name}")) == b""
assert term.encode(KeyEvent(name, action=KeyAction.RELEASE)) == b""

# XTMODKEYS level 2 leaves legacy F13-F35 and silent modifier keys untouched.
term.feed(b"\x1b[>4;2m")
assert term.modify_other_keys == 2
assert term.encode(KeyEvent("f13")) == b"\x1b[25~"
assert term.encode(KeyEvent.parse("ctrl+f13")) == b"\x1b[25;5~"
assert term.encode(KeyEvent.parse("ctrl+f21")) == b"\x1b[42;5~"
assert term.encode(KeyEvent("shift_left")) == b""
assert term.encode(KeyEvent.parse("ctrl+shift_left")) == b""
term.feed(b"\x1b[>4m")

# Standalone flags 4/16 keep legacy F13-F35 (no assoc text) and silent modifiers.
for standalone in (b"\x1b[>4u", b"\x1b[>16u"):
term.feed(standalone)
assert term.encode(KeyEvent("f13")) == b"\x1b[25~"
assert term.encode(KeyEvent("f13", text="x")) == b"\x1b[25~"
assert term.encode(KeyEvent("shift_left")) == b""
term.feed(b"\x1b[<1u")

# DISAMBIGUATE (1) and REPORT_EVENT_TYPES (2) encode F13-F35 as Kitty CSI u
# while keeping modifier-only keys silent until REPORT_ALL_KEYS (8) is active.
term.feed(b"\x1b[>1u")
assert term.encode(KeyEvent("f13")) == b"\x1b[57376u"
assert term.encode(KeyEvent("F35")) == b"\x1b[57398u"
assert term.encode(KeyEvent.parse("ctrl+f13")) == b"\x1b[57376;5u"
assert term.encode(KeyEvent("shift_left")) == b""
term.feed(b"\x1b[<1u")

term.feed(b"\x1b[>2u")
assert term.encode(KeyEvent("f13")) == b"\x1b[57376u"
assert term.encode(KeyEvent("f13", action=KeyAction.REPEAT)) == b"\x1b[57376;1:2u"
assert term.encode(KeyEvent("f13", action=KeyAction.RELEASE)) == b"\x1b[57376;1:3u"
assert term.encode(KeyEvent("shift_left", action=KeyAction.RELEASE)) == b""
term.feed(b"\x1b[<1u")

# REPORT_ALL_KEYS (8): all 12 left/right modifiers and F13-F35 emit Kitty CSI u.
term.feed(b"\x1b[>8u")
for name, cp in expected_kitty_fkeys.items():
assert term.encode(KeyEvent(name)) == f"\x1b[{cp}u".encode("ascii")
for name, cp in expected_modifiers.items():
assert term.encode(KeyEvent(name)) == f"\x1b[{cp}u".encode("ascii")
assert term.encode(KeyEvent(name.upper())) == f"\x1b[{cp}u".encode("ascii")
assert term.encode(KeyEvent(name, action=KeyAction.RELEASE)) == b""
assert (
term.encode(KeyEvent("shift_left", modifiers=KeyMod.SHIFT)) == b"\x1b[57441;2u"
)
assert term.encode(KeyEvent.parse("ctrl+shift_left")) == b"\x1b[57441;5u"
# AUTO_REPEAT off suppresses modifier/F13 repeat when REPORT_EVENT_TYPES is off.
term.feed(b"\x1b[?8l")
assert term.encode(KeyEvent("shift_left", action=KeyAction.REPEAT)) == b""
assert term.encode(KeyEvent("f13", action=KeyAction.REPEAT)) == b""
term.feed(b"\x1b[?8h\x1b[<1u")

# All flags (31): event types on modifiers/F13; no associated text on modifiers.
term.feed(b"\x1b[>31u")
assert (
term.encode(KeyEvent("shift_left", action=KeyAction.PRESS, text="x"))
== b"\x1b[57441u"
)
assert (
term.encode(
KeyEvent(
"ctrl_right",
modifiers=KeyMod.CTRL | KeyMod.SHIFT,
action=KeyAction.RELEASE,
)
)
== b"\x1b[57448;6:3u"
)
assert (
term.encode(KeyEvent("alt_left", action=KeyAction.REPEAT)) == b"\x1b[57443;1:2u"
)
assert (
term.encode(KeyEvent("f13", action=KeyAction.PRESS, text="x"))
== b"\x1b[57376;;120u"
)
assert (
term.encode(KeyEvent("f13", action=KeyAction.RELEASE, text="x"))
== b"\x1b[57376;1:3u"
)
# Functional and modifier keys omit alternate-key subparameters under flag 4.
assert (
term.encode(
KeyEvent("f13", modifiers=KeyMod.SHIFT, shifted_key="X", base_key="x")
)
== b"\x1b[57376;2u"
)
assert (
term.encode(
KeyEvent(
"shift_left", modifiers=KeyMod.SHIFT, shifted_key="X", base_key="x"
)
)
== b"\x1b[57441;2u"
)

# Direct encode_input() matches Terminal.encode().
assert (
encode_input(
KeyEvent("meta_right"),
modes=term.modes,
kitty_flags=KittyKeyboardFlags.REPORT_ALL_KEYS,
)
== b"\x1b[57452u"
)

# Out-of-range function keys and non-canonical modifiers raise ValueError.
for invalid in ("f0", "f36", "shift", "ctrl", "left_shift", "shift_center"):
with pytest.raises(ValueError, match="unsupported key identifier"):
term.encode(KeyEvent(invalid))
Loading