diff --git a/CHANGES b/CHANGES index a98244d3..76574a7f 100644 --- a/CHANGES +++ b/CHANGES @@ -62,6 +62,12 @@ Both errors exit with status 2. `vt_snapshot`. The fixture is also available when the project explicitly loads the Syrupy and libvt plugin modules in `conftest.py`. +#### Encode Kitty F3 and special-key releases (#176) + +Kitty disambiguation and event reporting use the F3 tilde form to +distinguish it from a cursor report. Enter, Tab, and Backspace release +events require the report-all-keys flag. + ### Documentation #### Assert effective colors alongside screen goldens (#190) @@ -69,6 +75,11 @@ loads the Syrupy and libvt plugin modules in `conftest.py`. The {doc}`/topics/testing` guide shows how to resolve a cell's color against the palette in its committed snapshot. +#### Request Kitty key events with their associated text (#176) + +The {doc}`/topics/protocols-and-input` guide shows how to request a key +press with its generated text and report its release. + ### Development #### Design review and issue skills (#155) diff --git a/MIGRATION b/MIGRATION index d3f0aa31..0c101d77 100644 --- a/MIGRATION +++ b/MIGRATION @@ -20,6 +20,18 @@ The tmux profile carries the current cursor, including pending wrap, into the blank alternate screen. Send `CSI H` after entering if the application draws from the top left. +### Kitty F3 (#82) + +Expect the `CSI 13 ~` form for F3 when disambiguation or event reporting +is active. Modifiers and event types occupy the second parameter. + +### Kitty F3 and special-key releases (#82, #118) + +Expect the `CSI 13 ~` form for F3 when disambiguation or event reporting +is active. Modifiers and event types occupy the second parameter. Combine +`REPORT_EVENT_TYPES` with `REPORT_ALL_KEYS` to receive Enter, Tab, and +Backspace releases. + ## libvt 0.0.1a2 (2026-11-28) ### Curated PEP 562 `libvt` root exports (#43) diff --git a/docs/topics/protocols-and-input.md b/docs/topics/protocols-and-input.md index 184e9f27..6f415717 100644 --- a/docs/topics/protocols-and-input.md +++ b/docs/topics/protocols-and-input.md @@ -134,6 +134,28 @@ b'\x1b[49:33;6u' b'\x1b[43;5u' ``` +Combine `REPORT_ALL_KEYS` with `REPORT_ASSOCIATED_TEXT` to receive an encoded +key press and its generated text together. Add `REPORT_EVENT_TYPES` to +receive releases. Release packets carry the key and action. +[Kitty keyboard flags](https://sw.kovidgoyal.net/kitty/keyboard-protocol/#progressive-enhancement) + +```python +>>> from libvt import Terminal +>>> from libvt.input import KeyAction, KeyEvent, KittyKeyboardFlags +>>> term = Terminal() +>>> flags = ( +... KittyKeyboardFlags.REPORT_ALL_KEYS +... | KittyKeyboardFlags.REPORT_ASSOCIATED_TEXT +... ) +>>> term.feed(f"\x1b[>{int(flags)}u".encode("ascii")) +>>> term.encode(KeyEvent("a", text="a")) +b'\x1b[97;;97u' +>>> flags |= KittyKeyboardFlags.REPORT_EVENT_TYPES +>>> term.feed(f"\x1b[>{int(flags)}u".encode("ascii")) +>>> term.encode(KeyEvent("a", action=KeyAction.RELEASE, text="a")) +b'\x1b[97;1:3u' +``` + ## Paste encoding By default, {func}`~libvt.input.encode_paste` (and diff --git a/src/libvt/input.py b/src/libvt/input.py index f0a6a42c..63dfd137 100644 --- a/src/libvt/input.py +++ b/src/libvt/input.py @@ -754,6 +754,8 @@ def _encode_key( b'\x1bOM' """ report_events = bool(kitty_flags & KittyKeyboardFlags.REPORT_EVENT_TYPES) + report_all = bool(kitty_flags & KittyKeyboardFlags.REPORT_ALL_KEYS) + disambig = report_all or bool(kitty_flags & KittyKeyboardFlags.DISAMBIGUATE) if event.action == KeyAction.RELEASE and not report_events: return b"" if ( @@ -782,6 +784,12 @@ def _encode_key( return f"\x1b[{final}".encode("ascii") return f"\x1b[1;{mod_param}{ev_suffix}{final}".encode("ascii") + # Kitty F3 uses a tilde so its reports cannot be mistaken for CPR. + if k_low == "f3" and (disambig or report_events): + return _format_kitty_csi( + 13, mod_param=mod_param, ev_suffix=ev_suffix, final="~" + ) + # SS3 F1-F4 if k_low in _SS3_FKEYS: final = _SS3_FKEYS[k_low] @@ -805,11 +813,6 @@ def _encode_key( final="~", ) - disambig = bool( - kitty_flags - & (KittyKeyboardFlags.DISAMBIGUATE | KittyKeyboardFlags.REPORT_ALL_KEYS), - ) - report_all = bool(kitty_flags & KittyKeyboardFlags.REPORT_ALL_KEYS) use_xtmod = kitty_flags == KittyKeyboardFlags.NONE and modify_other_keys in (1, 2) # Keypad keys (DECNKM ?66 / ESC = / ESC >) @@ -843,8 +846,7 @@ def _encode_key( if ( event.action == KeyAction.RELEASE and not report_all - and k_low not in ("escape", "esc") - and event.modifiers == KeyMod.NONE + and k_low in ("enter", "tab", "backspace") ): return b"" if ( diff --git a/tests/test_input.py b/tests/test_input.py index 50c07b5e..adcd744a 100644 --- a/tests/test_input.py +++ b/tests/test_input.py @@ -48,6 +48,41 @@ def test_parse_rejects_ambiguous_plus_chords(chord: str) -> None: KeyEvent.parse(chord) +@pytest.mark.parametrize("flags", [1, 2, 3, 8, 31]) +def test_kitty_f3_avoids_cursor_report(flags: int) -> None: + """Use the Kitty F3 tilde form for presses, repeats and releases.""" + term = libvt.Terminal(rows=10, cols=40) + assert term.encode(KeyEvent("f3")) == b"\x1bOR" + assert term.encode(KeyEvent("f3", KeyMod.CTRL)) == b"\x1b[1;5R" + term.feed(b"\x1b[1;5H\x1b[6n") + assert term.drain_replies() == b"\x1b[1;5R" + term.feed(f"\x1b[>{flags}u".encode("ascii")) + assert term.encode(KeyEvent("f3")) == b"\x1b[13~" + assert term.encode(KeyEvent("f3", KeyMod.CTRL)) == b"\x1b[13;5~" + repeat = b"\x1b[13;5:2~" if flags & 2 else b"\x1b[13;5~" + release = b"\x1b[13;5:3~" if flags & 2 else b"" + assert term.encode(KeyEvent("f3", KeyMod.CTRL, KeyAction.REPEAT)) == repeat + assert term.encode(KeyEvent("f3", KeyMod.CTRL, KeyAction.RELEASE)) == release + + +@pytest.mark.parametrize( + ("key", "code"), [("enter", 13), ("tab", 9), ("backspace", 127)] +) +def test_kitty_special_key_release_requires_report_all(key: str, code: int) -> None: + """Enter, Tab and Backspace releases require report-all with any modifier.""" + term = libvt.Terminal() + term.feed(b"\x1b[>3u") + assert term.encode(KeyEvent(key, action=KeyAction.RELEASE)) == b"" + release = KeyEvent(key, KeyMod.CTRL, KeyAction.RELEASE) + assert term.encode(release) == b"" + assert term.encode(KeyEvent(key, KeyMod.CTRL)) == f"\x1b[{code};5u".encode("ascii") + assert term.encode(KeyEvent("escape", action=KeyAction.RELEASE)) == b"\x1b[27;1:3u" + term.feed(b"\x1b[>31u") + assert term.encode(release) == f"\x1b[{code};5:3u".encode("ascii") + term.feed(b"\x1b[ None: """``KittyKeyboardStack`` isolates ``CSI u`` from ``SCORC`` and encodes input.""" term = libvt.Terminal(rows=4, cols=20)