Web backend calls preventDefault on every key, so the browser cannot handle Cmd+1–9 or reload
Checklist
- I have searched the issue tracker for open issues that relate to the same problem, before opening a new one.
- This issue only relates to a single bug. I will open new issues for any other problems.
Describe the bug
A Fyne app packaged for the web calls preventDefault() on every keydown and keyup. Chrome then skips shortcuts it still allows a page to override.
Blocked while the canvas is focused:
- Cmd+1 through Cmd+9 (jump to a browser tab). Ctrl+1–9 on other platforms.
- Cmd+R and Cmd+Shift+R (reload and force reload). Ctrl+R / Ctrl+Shift+R elsewhere.
- F5.
Ctrl+Tab still changes tabs. Chrome treats that shortcut as reserved and ignores preventDefault.
The call is in fyne-io/glfw-js v0.4.0, browser_wasm.go, in the document keydown and keyup listeners. The key callback is started with go and returns nothing, so an app cannot decline the event before preventDefault() runs. Canvas.AddShortcut only delivers shortcuts into the app. It cannot give the key back to the browser.
How to reproduce
- Package any Fyne app with
fyne package -os weband open it in Chrome on macOS. - Click the canvas so it has focus.
- Press Cmd+2. The browser stays on this tab.
- Press Cmd+Shift+R. The page does not reload.
- Press Ctrl+Tab. The browser switches tabs.
Example code
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/widget"
)
func main() {
a := app.New()
w := a.NewWindow("shortcut check")
w.SetContent(widget.NewLabel("Click here, then press Cmd+2 or Cmd+Shift+R"))
w.Resize(fyne.NewSize(480, 240))
w.ShowAndRun()
}Build with fyne package -os web and open the generated page. No shortcut has to be registered. The backend consumes the keys on its own.
Fyne version
2.8.1 (fyne.io/fyne/v2), with github.com/fyne-io/glfw-js v0.4.0
Go compiler version
go1.27.1
Operating system and version
macOS, Chrome. The same preventDefault() runs for every browser, so Ctrl+number and Ctrl+R are affected on Windows and Linux too.
Additional Information
A page can work around this with a capture-phase listener that calls stopImmediatePropagation() for those shortcuts before the GLFW listener runs. The fix belongs in glfw-js: skip preventDefault() for browser chrome shortcuts, or call it only when the app reports that it consumed the key. That decision has to happen in the event handler, before the goroutine.
Reported by Grok.
Source: fyne-io/fyne