wagoodman / keybinding

a golang wrapper for parsing gocui keybindings
MIT License
6 stars 7 forks source link

Adding single key as a shortcut #1

Open abitrolly opened 4 years ago

abitrolly commented 4 years ago

Apparently, single key bindings are not supported, and no error is reported for them.

https://github.com/wagoodman/dive/issues/129#issuecomment-568902936

abitrolly commented 4 years ago

To get to the root of the problem, I had to explore the code a bit. keybinding.Key object is a struct which includes gocui.Key object.

https://github.com/wagoodman/keybinding/blob/6a824da6df05a8a1e92332557efdd72181021c7d/keybinding.go#L111

gocui.Key is just an alias to termbox.Key

https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/keybinding.go#L44

termbox.Key is uint16.

https://github.com/nsf/termbox-go/blob/93860e16131719fa9722e7c448dbf8c0e3210a0d/api_common.go#L11

Now termbox.Key is used in termbox.Event struct with the interesting comment

type Event struct {
    Type   EventType // one of Event* constants
    Mod    Modifier  // one of Mod* constants or 0
    Key    Key       // one of Key* constants, invalid if 'Ch' is not 0
    Ch     rune      // a unicode character
...

https://github.com/nsf/termbox-go/blob/93860e16131719fa9722e7c448dbf8c0e3210a0d/api_common.go#L21-L22

There is only one event - EventKey - related to keyboard input. When parsing events, if input stream contains a simple character (not prefixed with escape sequence) , termbox doesn't assign event.Key field, and the character goes into event.Ch.

abitrolly commented 4 years ago

termbox examples process event.Key for key combinations and event.Ch for letters separately. gocui.newKeybinding also accepts key Key, ch rune as separate parameters.

https://github.com/jroimartin/gocui/blob/c055c87ae801372cd74a0839b972db4f7697ae5f/keybinding.go#L19

keybinding.Key struct however, has no placeholder for ch rune. keybinding.Parse method which should return Key doesn't handle chars and doesn't map them to any Key values.