Skip to content

Commit 84b68c5

Browse files
committed
feat: examples: update the print-key example to show the key text
1 parent 8085d33 commit 84b68c5

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

Diff for: examples/print-key/main.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,21 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
1717
case tea.KeyboardEnhancementsMsg:
1818
return m, tea.Printf("Keyboard enhancements enabled! ReleaseKeys: %v\n", msg.SupportsKeyReleases())
1919
case tea.KeyMsg:
20+
key := msg.Key()
2021
switch msg := msg.(type) {
2122
case tea.KeyPressMsg:
2223
switch msg.String() {
2324
case "ctrl+c":
2425
return m, tea.Quit
2526
}
2627
}
27-
return m, tea.Printf("You pressed: %s (%T)\n", msg.String(), msg)
28+
format := "(%T) You pressed: %s"
29+
args := []any{msg, msg.String()}
30+
if len(key.Text) > 0 {
31+
format += " (text: %q)"
32+
args = append(args, key.Text)
33+
}
34+
return m, tea.Printf(format, args...)
2835
}
2936
return m, nil
3037
}
@@ -34,7 +41,7 @@ func (m model) View() string {
3441
}
3542

3643
func main() {
37-
p := tea.NewProgram(model{}, tea.WithKeyboardEnhancements(tea.WithKeyReleases))
44+
p := tea.NewProgram(model{}, tea.WithKeyboardEnhancements(tea.WithKeyReleases, tea.WithUniformKeyLayout))
3845
if _, err := p.Run(); err != nil {
3946
log.Printf("Error running program: %v", err)
4047
}

0 commit comments

Comments
 (0)