awesome-gocui / gocui

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

Key binding doesn't seem to be working for runes #60

Open juliaschell opened 4 years ago

juliaschell commented 4 years ago

I was using jroimartin/gocui for a while and recently migrated to this fork.

When I type in one field of my gui, I intend to update another field according to what I wrote. I use a keybinding for this, effectively for each alphanumeric character I type. for _, c := range "{};=<>()[]'\"/\\-+:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" { g.SetKeybinding("Title", c, gocui.ModNone, create_update_titleBuff(c)) }

create_update_titleBuff writes the appropriate characters to each field I'm looking to edit

This no longer seems to be working. There is no error when setting the keybinding, but the create_update_titleBuff() does not seem to actually be called -- the other functionality within that function isn't happening. Instead, it appears as if I'm just typing into the Title field.

glvr182 commented 4 years ago

hmmm SetKeybinding requires a handler function SetKeybinding(viewname string, key interface{}, mod Modifier, handler func(*Gui, *View) error) error. So are you sure your code compiles?

Also is your code public? Might be able to have a look at it

juliaschell commented 4 years ago

Unfortunately my code is not public so I cannot share.

Yes the create_update_titleBuff(c) is my handler function. The code for that func is of the form

func create_update_titleBuff(c rune) func(g *gocui.Gui, v *gocui.View) error { 

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

                 /// update a few different views 

                 titleV, _ := g.View("Title")   

             titleV.EditWrite(rune('a'))

    }
}

With that code, I would expect my Title field to show 'aaaaaaa' as I type, but instead it just displays what I typed as if there were no key bindings at all and it was just a normal editable field

glvr182 commented 4 years ago

The issue is in

// matchView returns if the keybinding matches the current view.
func (kb *keybinding) matchView(v *View) bool {
    // if the user is typing in a field, ignore char keys
    if v == nil || (v.Editable && kb.ch != 0) {
        return false
    }
    return kb.viewName == v.name
}

Essentially this was designed to ignore keybinds when typing in an editable view. I'm thinking about making a bool called ignoreKB or something like that that defaults to true(ignore when editable) that you can set to false(do not ignore when editable) for your implementation. This way most people won't notice anything but you can implement your idea

glvr182 commented 4 years ago

Can you test my PR #62 You can do so by changing from awesome-gocui/gocui to glvr182/gocui OR just add the patch to your own code (module/vendor folder)