[GH-ISSUE #69] question: How to handle normal keys? #49

Closed
opened 2026-03-04 01:01:27 +03:00 by kerem · 2 comments
Owner

Originally created by @lesovsky on GitHub (Mar 10, 2018).
Original GitHub issue: https://github.com/rivo/tview/issues/69

Hi,

I've found in examples how to handle special keys, but nothing about normal, regular keys such as 'a','b','c', etc. Could you provide simple example?

Thanks

Originally created by @lesovsky on GitHub (Mar 10, 2018). Original GitHub issue: https://github.com/rivo/tview/issues/69 Hi, I've found in examples how to handle special keys, but nothing about normal, regular keys such as 'a','b','c', etc. Could you provide simple example? Thanks
kerem closed this issue 2026-03-04 01:01:27 +03:00
Author
Owner

@lesovsky commented on GitHub (Mar 10, 2018):

Answer is found on tcell godoc --
For normal keys, like ASCII letters, we use KeyRune, and then expect the application to inspect the Rune() member of the EventKey.

	app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		switch event.Key() {
		case tcell.KeyCtrlQ:
			app.Stop()
			os.Exit(0)
		case tcell.KeyRune:
			switch event.Rune() {
			case 'a':
				fmt.Fprintln(cmdl, "a is pressed")
			case 'F':
				fmt.Fprintln(cmdl, "F is pressed")
			default:
				fmt.Fprintln(cmdl, "rune is pressed")
			}
		}
		return event
	})
<!-- gh-comment-id:372007534 --> @lesovsky commented on GitHub (Mar 10, 2018): Answer is found on [tcell godoc](https://godoc.org/github.com/gdamore/tcell#Key) -- For normal keys, like ASCII letters, we use KeyRune, and then expect the application to inspect the Rune() member of the EventKey. ``` app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { switch event.Key() { case tcell.KeyCtrlQ: app.Stop() os.Exit(0) case tcell.KeyRune: switch event.Rune() { case 'a': fmt.Fprintln(cmdl, "a is pressed") case 'F': fmt.Fprintln(cmdl, "F is pressed") default: fmt.Fprintln(cmdl, "rune is pressed") } } return event }) ```
Author
Owner

@rivo commented on GitHub (Mar 10, 2018):

Yes, this is how it works. Glad you found it yourself. :-)

<!-- gh-comment-id:372026107 --> @rivo commented on GitHub (Mar 10, 2018): Yes, this is how it works. Glad you found it yourself. :-)
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#49
No description provided.