Skip to content

Commit

Permalink
feat: add kitty keyboard uniform key layout support
Browse files Browse the repository at this point in the history
This uses kitty keyboard protocol "Report alternate keys" and "Report
all keys as escape codes" to report key events as though they were on a
PC-101 layout. This is useful for uniform key event reporting across
different keyboard layouts.
  • Loading branch information
aymanbagabas committed Sep 20, 2024
1 parent a039baa commit 1be30d1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
24 changes: 24 additions & 0 deletions keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ func WithKeyReleases(k *keyboardEnhancements) {
k.kittyFlags |= ansi.KittyReportEventTypes
}

// WithUniformKeyLayout enables support for reporting key events as though they
// were on a PC-101 layout. This is useful for uniform key event reporting
// across different keyboard layouts. This is equivalent to the Kitty keyboard
// protocol "Report alternate keys" and "Report all keys as escape codes"
// progressive enhancement features.
//
// Note that not all terminals support this feature.
func WithUniformKeyLayout(k *keyboardEnhancements) {
k.kittyFlags |= ansi.KittyReportAlternateKeys | ansi.KittyReportAllKeysAsEscapeCodes
}

// withKeyDisambiguation enables support for disambiguating keyboard escape
// codes. This is useful for terminals that support the Kitty keyboard protocol
// "Disambiguate escape codes" progressive enhancement feature or the XTerm
Expand Down Expand Up @@ -102,3 +113,16 @@ func (k KeyboardEnhancementsMsg) SupportsKeyReleases() bool {
}
return k.kittyFlags&ansi.KittyReportEventTypes != 0
}

// SupportsKeyBaseLayout returns whether the terminal supports reporting key

This comment has been minimized.

Copy link
@meowgorithm

meowgorithm Sep 20, 2024

Member

SupportsUniformKeyLayout

This comment has been minimized.

Copy link
@aymanbagabas

aymanbagabas Sep 20, 2024

Author Member

Good catch

// events as though they were on a PC-101 layout.
func (k KeyboardEnhancementsMsg) SupportsUniformKeyLayout() bool {
if runtime.GOOS == "windows" {
// We use Windows Console API which supports reporting key events as
// though they were on a PC-101 layout.
return true
}
return k.SupportsKeyDisambiguation() && k.SupportsKeyReleases() &&
k.kittyFlags&ansi.KittyReportAlternateKeys != 0 &&
k.kittyFlags&ansi.KittyReportAllKeysAsEscapeCodes != 0
}
8 changes: 7 additions & 1 deletion kitty.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func fromKittyMod(mod int) KeyMod {
// CSI unicode-key-code:alternate-key-codes ; modifiers:event-type ; text-as-codepoints u
//
// See https://sw.kovidgoyal.net/kitty/keyboard-protocol/
func parseKittyKeyboard(csi *ansi.CsiSequence) Msg {
func parseKittyKeyboard(csi *ansi.CsiSequence) (msg Msg) {
var isRelease bool
key := Key{}

Expand Down Expand Up @@ -253,6 +253,12 @@ func parseKittyKeyboard(csi *ansi.CsiSequence) Msg {
}
}

if key.Mod == ModShift && key.ShiftedCode == 0 {
if unicode.IsLower(key.Code) {
key.ShiftedCode = unicode.ToUpper(key.Code)
}
}

noneOrShifted := key.Mod <= ModShift && key.Code != KeyLeftShift && key.Code != KeyRightShift
if len(key.Text) == 0 && noneOrShifted {
if key.ShiftedCode != 0 && unicode.IsPrint(key.ShiftedCode) {
Expand Down

0 comments on commit 1be30d1

Please sign in to comment.