Skip to content

nsgotest: BlurWindow leaves keys held with KeyCodeDown in KeyCodesDown #2248

Description

@houko

Context

nsgotest.App.BlurWindow is documented to release "every held key and held mouse button just as desktop and web platforms do on focus loss". For a key held with KeyCodeDown, the key-up it sends is wrong, so the key stays in core.KeyCodesDown and a view's OnKeyUp never hears its position come up.

In nsgotest/pointer.go (main, lines 772-776):

for name, h := range a.held {
	delete(a.held, name)
	if h.reported && h.tree != nil {
		h.tree.Dispatch(driver.Event{Kind: driver.EventKeyUp, ID: h.id, Text: name})
	}
}

a.held is keyed by identity, which press in nsgotest/steps.go sets to the physical code when the key was pressed with KeyCodeDown (identity := code). So the event carries Text: "KeyW" and no KeyCode. The normal path, release in nsgotest/steps.go, sends Text: h.name, KeyCode: h.code, which is what BlurWindow should send too.

Why it matters

A game that binds actions by physical position (WASD on any layout) reads core.KeyCodesDown and needs the focus-loss release to work, or the character keeps walking after the window loses focus. In nsgo-dev/game the input package's WatchKeys test has to hold keys by name instead of by code for the blur case, because the code-held variant cannot pass against BlurWindow today.

Minimal reproduction

var codes []string
app := nsgotest.Start(t, func(cx *core.Scope) core.Node {
	cx.Effect(func() { codes = core.KeyCodesDown(cx) })
	return core.View{}
})
app.KeyCodeDown("KeyW")
app.Settle() // codes == ["KeyW"]
app.BlurWindow()
app.Settle()
if len(codes) != 0 {
	t.Fatalf("KeyCodesDown after BlurWindow = %v, want none", codes) // fails today: [KeyW]
}

Proposal

In BlurWindow, dispatch driver.Event{Kind: driver.EventKeyUp, ID: h.id, Text: h.name, KeyCode: h.code}, matching release, and add a test next to the existing BlurWindow tests that holds a key with KeyCodeDown and checks KeyCodesDown, KeysDown and OnKeyUp after the blur.

Acceptance

  • After KeyCodeDown("KeyW") and BlurWindow(), core.KeyCodesDown is empty and OnKeyUp hears KeyEvent{Key: "w", Code: "KeyW"}.
  • A key held by name with KeyDown still comes up with its name, as today.

Activity

  1. self-assigned this
    on Nov 25, 2026
  2. added a commit that references this issue on Nov 25, 2026
    69b9a67
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions