Skip to content

Keyboard

Brandon Jordan edited this page Nov 10, 2024 · 6 revisions

Bind Keys

To bind keys, ttuy uses github.com/eiannone/keyboard, then the binding details with the struct.

BindKey(key keyboard.Key, binding KeyBinding)
// KeyBinding describes a key binding to an action.
type KeyBinding struct {
	// Description of what the handler will do.
	descriptor string

	// Representation of key (e.g. ^C).
	keyString string

	// Called when the key is pressed.
	handler func()
}

Example

BindKey(keyboard.KeyCtrlC, KeyBinding{
	descriptor: "Exit",
	keyString:  "^C",
	handler: func() {
		StopPainting()
		fmt.Print("Exited.\n\n")
	},
})

Print Key Bindings

To list keybindings, use PrintKeyBindings().

PrintKeyBindings(separator string)

This returns a styled string of the registered key bindings.

Example

^C Exit^Q Toggle highlight
Clone this wiki locally