2020-05-15 23:17:41 +00:00
|
|
|
package cview
|
|
|
|
|
2020-05-17 17:14:17 +00:00
|
|
|
import (
|
|
|
|
"github.com/gdamore/tcell"
|
|
|
|
"gitlab.com/tslocum/cbind"
|
|
|
|
)
|
|
|
|
|
2020-05-15 23:17:41 +00:00
|
|
|
// Key defines the keyboard shortcuts of an application.
|
|
|
|
type Key struct {
|
2020-05-19 02:49:25 +00:00
|
|
|
Select []string
|
|
|
|
SelectAlt []string // SelectAlt is also used when not focusing a text input.
|
|
|
|
Cancel []string
|
|
|
|
|
|
|
|
MoveUp []string
|
|
|
|
MoveDown []string
|
|
|
|
MoveLeft []string
|
|
|
|
MoveRight []string
|
|
|
|
|
|
|
|
MoveFirst []string
|
|
|
|
MoveLast []string
|
|
|
|
MovePreviousField []string
|
|
|
|
MoveNextField []string
|
|
|
|
MovePreviousPage []string
|
|
|
|
MoveNextPage []string
|
2020-05-15 23:17:41 +00:00
|
|
|
|
|
|
|
ShowContextMenu []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keys defines the keyboard shortcuts of an application.
|
|
|
|
var Keys = Key{
|
2020-05-19 02:49:25 +00:00
|
|
|
Select: []string{"Enter", "Ctrl+J"}, // Ctrl+J = keypad enter
|
|
|
|
SelectAlt: []string{"Space"},
|
|
|
|
Cancel: []string{"Escape"},
|
|
|
|
|
|
|
|
MoveUp: []string{"Up", "k"},
|
|
|
|
MoveDown: []string{"Down", "j"},
|
|
|
|
MoveLeft: []string{"Left", "h"},
|
|
|
|
MoveRight: []string{"Right", "l"},
|
|
|
|
|
|
|
|
MoveFirst: []string{"Home", "g"},
|
|
|
|
MoveLast: []string{"End", "G"},
|
|
|
|
MovePreviousField: []string{"Backtab"},
|
|
|
|
MoveNextField: []string{"Tab"},
|
|
|
|
MovePreviousPage: []string{"PageUp", "Ctrl+B"},
|
|
|
|
MoveNextPage: []string{"PageDown", "Ctrl+F"},
|
2020-05-15 23:17:41 +00:00
|
|
|
|
|
|
|
ShowContextMenu: []string{"Alt+Enter"},
|
|
|
|
}
|
2020-05-17 17:14:17 +00:00
|
|
|
|
|
|
|
// HitShortcut returns whether the EventKey provided is present in one or more
|
|
|
|
// sets of keybindings.
|
|
|
|
func HitShortcut(event *tcell.EventKey, keybindings ...[]string) bool {
|
|
|
|
enc, err := cbind.Encode(event.Modifiers(), event.Key(), event.Rune())
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, binds := range keybindings {
|
|
|
|
for _, key := range binds {
|
|
|
|
if key == enc {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|