Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cursed_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ func (s *cursedRenderer) start() {
if s.lastView.KeyboardEnhancements.ReportEventTypes {
kittyFlags |= ansi.KittyReportEventTypes
}
if s.lastView.KeyboardEnhancements.ReportAllKeysAsEscapeCodes {
kittyFlags |= ansi.KittyReportAllKeysAsEscapeCodes
}
_, _ = s.scr.WriteString(ansi.KittyKeyboard(kittyFlags, 1))
}

Expand Down Expand Up @@ -383,6 +386,9 @@ func (s *cursedRenderer) flush(closing bool) error {
if view.KeyboardEnhancements.ReportEventTypes {
kittyFlags |= ansi.KittyReportEventTypes
}
if view.KeyboardEnhancements.ReportAllKeysAsEscapeCodes {
kittyFlags |= ansi.KittyReportAllKeysAsEscapeCodes
}
_, _ = s.scr.WriteString(ansi.KittyKeyboard(kittyFlags, 1))
if !closing {
// Request keyboard enhancements when they change
Expand Down
13 changes: 12 additions & 1 deletion examples/keyboard-enhancements/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main
import (
"fmt"
"os"
"slices"
"strings"

tea "charm.land/bubbletea/v2"
Expand All @@ -19,6 +20,8 @@ type styles struct {
type model struct {
supportsDisambiguation bool
supportsEventTypes bool
supportsAllKeys bool
allKeysAsEscapeCodes bool
styles styles
}

Expand Down Expand Up @@ -46,6 +49,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// Check which features were able to be enabled.
m.supportsDisambiguation = true // This is always enabled when this msg is received.
m.supportsEventTypes = msg.SupportsEventTypes()
m.supportsAllKeys = msg.SupportsAllKeysAsEscapeCodes()

case tea.KeyPressMsg:
switch msg.String() {
Expand All @@ -70,6 +74,10 @@ func (m model) View() tea.View {
var b strings.Builder
fmt.Fprintf(&b, "Terminal supports key releases: %v\n", m.supportsEventTypes)
fmt.Fprintf(&b, "Terminal supports key disambiguation: %v\n", m.supportsDisambiguation)
fmt.Fprintf(&b, "Terminal supports all keys as escape codes: %v\n", m.supportsAllKeys)
if m.allKeysAsEscapeCodes {
fmt.Fprint(&b, "Mode: all keys as escape codes (--all-keys)\n")
}
fmt.Fprint(&b, "This demo logs key events. Press ctrl+c to quit.")
v.SetContent(b.String() + "\n")

Expand All @@ -78,6 +86,7 @@ func (m model) View() tea.View {
// the ability to distinguish between certain key presses like "enter" and
// "shift+enter" or "tab" and "ctrl+i".
v.KeyboardEnhancements.ReportEventTypes = true
v.KeyboardEnhancements.ReportAllKeysAsEscapeCodes = m.allKeysAsEscapeCodes

return v
}
Expand All @@ -95,7 +104,9 @@ func (m *model) updateStyles(isDark bool) {
}

func initialModel() model {
m := model{}
m := model{
allKeysAsEscapeCodes: slices.Contains(os.Args[1:], "--all-keys"),
}
m.updateStyles(true) // default to dark styles.
return m
}
Expand Down
6 changes: 6 additions & 0 deletions keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ func (k KeyboardEnhancementsMsg) SupportsKeyDisambiguation() bool {
func (k KeyboardEnhancementsMsg) SupportsEventTypes() bool {
return k.Flags&ansi.KittyReportEventTypes != 0
}

// SupportsAllKeysAsEscapeCodes returns whether the terminal supports
// reporting all key events as escape codes, including printable characters.
func (k KeyboardEnhancementsMsg) SupportsAllKeysAsEscapeCodes() bool {
return k.Flags&ansi.KittyReportAllKeysAsEscapeCodes != 0
}
8 changes: 8 additions & 0 deletions tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@ type KeyboardEnhancements struct {
// [KeyPressMsg] with the [Key.IsRepeat] field set indicating that this is
// a it's part of a key repeat sequence.
ReportEventTypes bool

// ReportAllKeysAsEscapeCodes requests the terminal to report all key
// events as escape codes, including printable characters like Space. This
// is useful when you need accurate modifier information for all keys.
// Without this, keys like Shift+Space may not report the Shift modifier
// on press because the terminal sends the raw character byte which
// carries no modifier information.
ReportAllKeysAsEscapeCodes bool
}

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