bubbletea/keyboard.go
Ayman Bagabas 05df5aecf8
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: https://github.com/charmbracelet/bubbletea/pull/1622
Fixes: https://github.com/charmbracelet/bubbletea/issues/1621
Fixes: https://github.com/charmbracelet/bubbletea/issues/1623
2026-04-07 11:03:32 -04:00

59 lines
1.9 KiB
Go

package tea
import (
"github.com/charmbracelet/x/ansi"
)
// KeyboardEnhancementsMsg is a message that gets sent when the terminal
// supports keyboard enhancements.
type KeyboardEnhancementsMsg struct {
// Flags is a bitmask of enabled keyboard enhancement features. A non-zero
// value indicates that at least we have key disambiguation support.
//
// See [ansi.KittyReportEventTypes] and other constants for details.
//
// Example:
//
// ```go
// // The hard way
// if msg.Flags&ansi.KittyReportEventTypes != 0 {
// // Terminal supports reporting different key event types
// }
//
// // The easy way
// if msg.SupportsEventTypes() {
// // Terminal supports reporting different key event types
// }
// ```
Flags int
}
// SupportsKeyDisambiguation returns whether the terminal supports key
// disambiguation (e.g., distinguishing between different modifier keys).
func (k KeyboardEnhancementsMsg) SupportsKeyDisambiguation() bool {
return k.Flags > 0
}
// SupportsEventTypes returns whether the terminal supports reporting
// different types of key events (press, release, and repeat).
func (k KeyboardEnhancementsMsg) SupportsEventTypes() bool {
return k.Flags&ansi.KittyReportEventTypes != 0
}
// SupportsAlternateKeys returns whether the terminal supports reporting
// alternate key codes.
func (k KeyboardEnhancementsMsg) SupportsAlternateKeys() bool {
return k.Flags&ansi.KittyReportAlternateKeys != 0
}
// SupportsAllKeysAsEscapeCodes returns whether the terminal supports reporting
// all keys as escape codes.
func (k KeyboardEnhancementsMsg) SupportsAllKeysAsEscapeCodes() bool {
return k.Flags&ansi.KittyReportAllKeysAsEscapeCodes != 0
}
// SupportsAssociatedText returns whether the terminal supports reporting
// associated text with key events.
func (k KeyboardEnhancementsMsg) SupportsAssociatedText() bool {
return k.Flags&ansi.KittyReportAssociatedKeys != 0
}