0

I have overridden an Entry to deal with a tab keypress in a fyne Table. My aim is to finish editing and go to the next cell when the Tab key is pressed, just like in Excel. However it seems like Tab is absorbed by the framework. Other keys like arrows and enter are picked up by my TypeKey event handler, as I have observed from the fmt.Println("TypedKey", event.Name) at the start of the event handler.

Currently my OnSubmitted event handler is only called when Enter is pressed, and not tab. Tab just seems to finish editing and switch to the Label without calling OnSubmitted.

Here is the code for the overidden Entry:

type EditingEntry struct {
    widget.Entry
    OnTab         func()
}

func NewEditingEntry() *EditingEntry {
    e := &EditingEntry{}
    e.ExtendBaseWidget(e)
    return e
}

func (e *EditingEntry) TypedKey(event *fyne.KeyEvent) {
    fmt.Println("TypedKey", event.Name)
    if event.Name == fyne.KeyTab {
        if e.OnTab != nil {
            e.OnTab()
        }
        return 
    }
    e.Entry.TypedKey(event) // Call base behavior
}

I am using version fyne.io/fyne/v2 v2.6.1 on Ubuntu linux 22.04.5, with XWindows, KDE. The Table uses widget.Stack for the cells, which contains widget.Label for the non-editing cells and EditingEntry for the editing cell.

1 Answer 1

0

I stumbled across the solution while composing the question, but I thought I'd continue to post the question as it might be helpful to others to be aware of the answer:

There is a Tabbable interface in Fyne since v2.1.0. I just needed to implement that interface for my EditingEntry like below to start receiving the tab events:

// Implement the Tabbable interface
func (e *EditingEntry) AcceptsTab() bool {
    // Return true to receive Tab key events
    return true
}

Now the challenge is how to detect Shift-Tab!

After a pointer from Andy in the comments, to detect shift I need to get the desktop driver, after importing "fyne.io/fyne/v2/driver/desktop":

var driver desktop.Driver
if drv, ok := myApp.Driver().(desktop.Driver); ok {
    driver = drv
}

Then in the event handler get the modifiers:

if driver != nil {
   modifiers := driver.CurrentKeyModifiers()
   fmt.Println(modifiers)
}

Note that in my environment, this works for Shift-Tab, but not for ctrl or alt-tab, the Tab is not detected while I'm holding shift or ctrl.

Sign up to request clarification or add additional context in comments.

2 Comments

Glad you found it. You can ask the driver which keyboard modifiers are held (see CurrentKeyModifiers on the desktop.Driver extension)
Thanks, and thanks for fyne, I'm really enjoying having a native GUI for my go program.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.