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

Keybinding on gocui.KeyEsc does not seem to work #55

Closed ERnsTL closed 8 years ago

ERnsTL commented 8 years ago

Greetings, I just wanted to catch the Escape key via a key binding, but it did not work. I used other key bindings, so they generally seem to work. I tried in different terminal emulators, checked escape sequence options. System here: recent Ubuntu Linux.

Copy-paste test program:

package main

import (
    "fmt"
    "log"

    "github.com/jroimartin/gocui"
)

func main() {
    g := gocui.NewGui()
    if err := g.Init(); err != nil {
        log.Panicln(err)
    }
    defer g.Close()

    g.SetLayout(layout)
    if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
        log.Panicln(err)
    }
    if err := g.SetKeybinding("main", gocui.KeyEnter, gocui.ModNone, enterKey); err != nil {
        log.Panicln(err)
    }
    if err := g.SetKeybinding("main", gocui.KeyEsc, gocui.ModNone, escKey); err != nil {
        log.Panicln(err)
    }

    if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
        log.Panicln(err)
    }
}

func layout(g *gocui.Gui) error {
    if v, err := g.SetView("main", 0, 0, 50, 10); err != nil {
        if err != gocui.ErrUnknownView {
            return err
        }
        v.Frame = true
        v.Editable = true
        if err := g.SetCurrentView("main"); err != nil {
            return err
        }
    }
    return nil
}

func quit(g *gocui.Gui, v *gocui.View) error {
    return gocui.ErrQuit
}

func escKey(g *gocui.Gui, v *gocui.View) error {
    fmt.Fprint(v, "ESC KEY")
    return nil
}

func enterKey(g *gocui.Gui, v *gocui.View) error {
    fmt.Fprint(v, "ENTER KEY")
    return nil
}

Maybe this is just a wrong keycode taken over from termbox, I dont know.

jroimartin commented 8 years ago

Please, check the following comment:

https://github.com/jroimartin/gocui/issues/47#issuecomment-213737189

Does it answer your question? If not, please let me know :)

ERnsTL commented 8 years ago

Thanks for the pointer! Catching Esc key + number key 2 works using the solution described there:

    if err := g.SetKeybinding("", '2', gocui.ModAlt, escKey); err != nil {
        log.Panicln(err)
    }

That probably covers all key combinations involving Esc, good to know!

Though how can I write a key binding for the Esc key alone? Consequently, I tried leaving out the '2', like so

    if err := g.SetKeybinding("", '', gocui.ModAlt, escKey); err != nil {
        log.Panicln(err)
    }

but it does not work. Is there any way to handle the Esc key alone?

Would like to be able to close a view on Esc key, and exit a window/dialog/interaction chain using an Esc key press.

jroimartin commented 8 years ago

This is now possible thanks to #56. Give a look to the PR :) Also, you have more details here:

https://godoc.org/github.com/nsf/termbox-go#InputMode

jameycribbs commented 8 years ago

Excellent! Thanks so much for this! This gets cribbnotes_cui that much closer to feel like it has a real vim mode!