From c9c500d4496d3741b4247e3ffb280e8aa8e8ae98 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 2 Dec 2026 19:16:22 -0600 Subject: [PATCH 1/4] input(fix[kitty]): Use tilde for enhanced F3 why: A modified F3 can have the same bytes as a cursor-position reply. what: - Encode enhanced F3 with CSI 13 ~ and retain legacy SS3. - Test presses, repeats and releases alongside a cursor report. Fixes #82 --- MIGRATION | 5 +++++ src/libvt/input.py | 13 ++++++++----- tests/test_input.py | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/MIGRATION b/MIGRATION index d3f0aa31..1893a6bb 100644 --- a/MIGRATION +++ b/MIGRATION @@ -20,6 +20,11 @@ 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. + ## libvt 0.0.1a2 (2026-11-28) ### Curated PEP 562 `libvt` root exports (#43) diff --git a/src/libvt/input.py b/src/libvt/input.py index f0a6a42c..e8092740 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 >) diff --git a/tests/test_input.py b/tests/test_input.py index 50c07b5e..c50ea21c 100644 --- a/tests/test_input.py +++ b/tests/test_input.py @@ -48,6 +48,23 @@ 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 + + def test_kitty_keyboard_stack_mouse_sgr_and_focus_encoding() -> None: """``KittyKeyboardStack`` isolates ``CSI u`` from ``SCORC`` and encodes input.""" term = libvt.Terminal(rows=4, cols=20) From ca864f23953138d811935ff4ee0f002b63abaf04 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 2 Dec 2026 19:16:22 -0600 Subject: [PATCH 2/4] input(fix[kitty]): Gate special-key releases why: Modifiers alone do not request Enter, Tab or Backspace releases. what: - Suppress those releases until the report-all flag is set. - Check both flag combinations and ordinary Escape release. Fixes #118 --- MIGRATION | 7 +++++++ src/libvt/input.py | 3 +-- tests/test_input.py | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/MIGRATION b/MIGRATION index 1893a6bb..0c101d77 100644 --- a/MIGRATION +++ b/MIGRATION @@ -25,6 +25,13 @@ application draws from the top left. 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/src/libvt/input.py b/src/libvt/input.py index e8092740..63dfd137 100644 --- a/src/libvt/input.py +++ b/src/libvt/input.py @@ -846,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 c50ea21c..adcd744a 100644 --- a/tests/test_input.py +++ b/tests/test_input.py @@ -65,6 +65,24 @@ def test_kitty_f3_avoids_cursor_report(flags: int) -> None: 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) From 3efed03faaaa6975226ac815a28a1294257112a5 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 2 Dec 2026 19:16:23 -0600 Subject: [PATCH 3/4] docs(input): Show Kitty associated text why: Applications must request associated text along with key events. what: - Show the flag combination and resulting press and release bytes. Fixes #130 --- docs/topics/protocols-and-input.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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 From bf79e6781e31ae70da9efe5c6d14afb3f445fe56 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 2 Dec 2026 19:42:45 -0600 Subject: [PATCH 4/4] CHANGES(docs): Explain Kitty key reports why: Kitty F3 shares a letter with cursor reports, and some release events require all-key reporting. what: - Document the F3 form, release flags and associated-text example. --- CHANGES | 11 +++++++++++ 1 file changed, 11 insertions(+) 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)