Skip to content

Commit 05df5ae

Browse files
authored
feat: support extended keyboard enhancements (#1626)
This adds support for the Kitty keyboard protocol's extended enhancements, allowing applications to request that the terminal report additional information about key events, such as whether they're part of a repeat sequence, alternate key values, and associated text. Supersedes: #1622 Fixes: #1621 Fixes: #1623
1 parent ee06e98 commit 05df5ae

3 files changed

Lines changed: 56 additions & 8 deletions

File tree

cursed_renderer.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,7 @@ func (s *cursedRenderer) start() {
131131
// Both can coexist; terminals ignore what they don't support.
132132
_, _ = s.scr.WriteString(ansi.SetModifyOtherKeys2)
133133

134-
kittyFlags := ansi.KittyDisambiguateEscapeCodes
135-
if s.lastView.KeyboardEnhancements.ReportEventTypes {
136-
kittyFlags |= ansi.KittyReportEventTypes
137-
}
134+
kittyFlags := keyboardEnhancementsFlags(s.lastView.KeyboardEnhancements)
138135
_, _ = s.scr.WriteString(ansi.KittyKeyboard(kittyFlags, 1))
139136
}
140137

@@ -379,10 +376,7 @@ func (s *cursedRenderer) flush(closing bool) error {
379376
// Enable modifyOtherKeys and Kitty keyboard protocol.
380377
_, _ = s.scr.WriteString(ansi.SetModifyOtherKeys2)
381378

382-
kittyFlags := ansi.KittyDisambiguateEscapeCodes // always enable basic key disambiguation
383-
if view.KeyboardEnhancements.ReportEventTypes {
384-
kittyFlags |= ansi.KittyReportEventTypes
385-
}
379+
kittyFlags := keyboardEnhancementsFlags(view.KeyboardEnhancements)
386380
_, _ = s.scr.WriteString(ansi.KittyKeyboard(kittyFlags, 1))
387381
if !closing {
388382
// Request keyboard enhancements when they change
@@ -828,3 +822,20 @@ func viewEquals(a, b *View) bool {
828822

829823
return true
830824
}
825+
826+
func keyboardEnhancementsFlags(ke KeyboardEnhancements) int {
827+
flags := 1 // always enable basic key disambiguation
828+
if ke.ReportEventTypes {
829+
flags |= ansi.KittyReportEventTypes
830+
}
831+
if ke.ReportAlternateKeys {
832+
flags |= ansi.KittyReportAlternateKeys
833+
}
834+
if ke.ReportAllKeysAsEscapeCodes {
835+
flags |= ansi.KittyReportAllKeysAsEscapeCodes
836+
}
837+
if ke.ReportAssociatedText {
838+
flags |= ansi.KittyReportAssociatedKeys
839+
}
840+
return flags
841+
}

keyboard.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,21 @@ func (k KeyboardEnhancementsMsg) SupportsKeyDisambiguation() bool {
3939
func (k KeyboardEnhancementsMsg) SupportsEventTypes() bool {
4040
return k.Flags&ansi.KittyReportEventTypes != 0
4141
}
42+
43+
// SupportsAlternateKeys returns whether the terminal supports reporting
44+
// alternate key codes.
45+
func (k KeyboardEnhancementsMsg) SupportsAlternateKeys() bool {
46+
return k.Flags&ansi.KittyReportAlternateKeys != 0
47+
}
48+
49+
// SupportsAllKeysAsEscapeCodes returns whether the terminal supports reporting
50+
// all keys as escape codes.
51+
func (k KeyboardEnhancementsMsg) SupportsAllKeysAsEscapeCodes() bool {
52+
return k.Flags&ansi.KittyReportAllKeysAsEscapeCodes != 0
53+
}
54+
55+
// SupportsAssociatedText returns whether the terminal supports reporting
56+
// associated text with key events.
57+
func (k KeyboardEnhancementsMsg) SupportsAssociatedText() bool {
58+
return k.Flags&ansi.KittyReportAssociatedKeys != 0
59+
}

tea.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,25 @@ type KeyboardEnhancements struct {
245245
// [KeyPressMsg] with the [Key.IsRepeat] field set indicating that this is
246246
// a it's part of a key repeat sequence.
247247
ReportEventTypes bool
248+
249+
// ReportAlternateKeys requests the terminal to report alternate key values
250+
// in addition to the main ones.
251+
// Note that only key events represented as escape codes will affected by
252+
// this enhancement.
253+
ReportAlternateKeys bool
254+
255+
// ReportAllKeysAsEscapeCodes requests the terminal to report all key
256+
// events, including plain text keys, as escape codes.
257+
// When this is enabled, text won't be sent as plain text but instead as
258+
// escape codes that encode the key value and modifiers.
259+
ReportAllKeysAsEscapeCodes bool
260+
261+
// ReportAssociatedText requests the terminal to report the text associated
262+
// with key events.
263+
// Note that this is an enhancement to
264+
// [KeyboardEnhancements.ReportAllKeysAsEscapeCodes] and only has an effect
265+
// if that is enabled.
266+
ReportAssociatedText bool
248267
}
249268

250269
// SetContent is a helper method to set the content of a [View] with a styled

0 commit comments

Comments
 (0)