Skip to content

Commit

Permalink
feat: examples: update the print-key example to show the key text
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Sep 20, 2024
1 parent 7800346 commit 6073691
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions examples/print-key/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@ 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() {
case "ctrl+c":
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
}
Expand All @@ -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)
}
Expand Down

0 comments on commit 6073691

Please sign in to comment.