From d2bffc5a3fb471251f4bb4ed08ea8842171089c4 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Fri, 20 Sep 2024 15:54:31 -0400 Subject: [PATCH] feat: examples: update the print-key example to show the key text --- examples/print-key/main.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/examples/print-key/main.go b/examples/print-key/main.go index 409c2b93ef..4a99b26379 100644 --- a/examples/print-key/main.go +++ b/examples/print-key/main.go @@ -17,6 +17,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tea.KeyboardEnhancementsMsg: return m, tea.Printf("Keyboard enhancements enabled! ReleaseKeys: %v\n", msg.SupportsKeyReleases()) case tea.KeyMsg: + key := msg.Key() switch msg := msg.(type) { case tea.KeyPressMsg: switch msg.String() { @@ -24,7 +25,13 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, tea.Quit } } - return m, tea.Printf("You pressed: %s (%T)\n", msg.String(), msg) + format := "(%T) You pressed: %s" + args := []any{msg, msg.String()} + if len(key.Text) > 0 { + format += " (text: %q)" + args = append(args, key.Text) + } + return m, tea.Printf(format, args...) } return m, nil } @@ -34,7 +41,7 @@ func (m model) View() string { } func main() { - p := tea.NewProgram(model{}, tea.WithKeyboardEnhancements(tea.WithKeyReleases)) + p := tea.NewProgram(model{}, tea.WithKeyboardEnhancements(tea.WithKeyReleases, tea.WithKeyBaseLayout)) if _, err := p.Run(); err != nil { log.Printf("Error running program: %v", err) }