jroimartin / gocui

Minimalist Go package aimed at creating Console User Interfaces.
BSD 3-Clause "New" or "Revised" License
9.92k stars 608 forks source link

Question: Default handler for all keybindings. #159

Open lesovsky opened 6 years ago

lesovsky commented 6 years ago

Hi,

Is there a way to define default handler function for all keybindings which are not defined separately? For example, a program has a set of keybindings with associated handlers, and also there are other keybindings that are not associated with any functions. When user presses all another keys, program should run default handler which should display something like: 'unknown command -- try "h" for help'

is it possible?

michalkowalik commented 6 years ago

I hope it's OK if I comment on it. One possible option, which I'm using is to write your own editor function. It may be as simple as sending the keystroke to the channel:

func ed(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
  keystrokes <- ch
}

and processing the keys / runes any way you want, for example:

// read from keyboard:
  go func() {
    for {
        k := <-keystrokes
        g.Update(func(g *gocui.Gui) error {
            v, _ := g.View(termName)
            fmt.Fprintf(v, "KEY -> %s\n", string(k))
            v.MoveCursor(0, 1, true)
            return nil
        })
    }
}()