From abbfe190253f588b8426e6de0a8836753873e8f8 Mon Sep 17 00:00:00 2001 From: "J4ck1e:)" Date: Tue, 24 Nov 2026 23:06:04 +0800 Subject: [PATCH] fix(nsgotest): release physical keys on window blur Send the held key's original logical name and physical code when the headless test window loses focus so both held-key trackers receive the release. Fixes #2248 --- changelog.d/blur-releases-physical-keys.md | 3 +++ nsgotest/pointer.go | 6 ++--- nsgotest/steps_test.go | 30 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 changelog.d/blur-releases-physical-keys.md diff --git a/changelog.d/blur-releases-physical-keys.md b/changelog.d/blur-releases-physical-keys.md new file mode 100644 index 000000000..5a709a97e --- /dev/null +++ b/changelog.d/blur-releases-physical-keys.md @@ -0,0 +1,3 @@ +### Fixed + +- Release physical keys with their original name and code when `nsgotest.App.BlurWindow` simulates focus loss (#2248 by @J4ck1e2000). diff --git a/nsgotest/pointer.go b/nsgotest/pointer.go index 84ec44e6f..e55bba56e 100644 --- a/nsgotest/pointer.go +++ b/nsgotest/pointer.go @@ -769,10 +769,10 @@ func (a *App) BlurWindow() { a.done() return } - for name, h := range a.held { - delete(a.held, name) + for identity, h := range a.held { + delete(a.held, identity) if h.reported && h.tree != nil { - h.tree.Dispatch(driver.Event{Kind: driver.EventKeyUp, ID: h.id, Text: name}) + h.tree.Dispatch(driver.Event{Kind: driver.EventKeyUp, ID: h.id, Text: h.name, KeyCode: h.code}) } } for b := driver.MouseButtonLeft; b <= driver.MouseButtonForward; b++ { diff --git a/nsgotest/steps_test.go b/nsgotest/steps_test.go index 324badbcb..9c47b1b8d 100644 --- a/nsgotest/steps_test.go +++ b/nsgotest/steps_test.go @@ -176,6 +176,36 @@ func TestPhysicalKeyStepsFollowThePositionAndLayout(t *testing.T) { } } +func TestBlurWindowReleasesNamedAndPhysicalKeys(t *testing.T) { + var keys, codes []string + var ups []core.KeyEvent + app := Start(t, func(cx *core.Scope) core.Node { + cx.Effect(func() { + keys = core.KeysDown(cx) + codes = core.KeyCodesDown(cx) + }) + return core.View{ + TestID: "pad", Focusable: func() bool { return true }, + OnKeyUp: func(e core.KeyEvent) { ups = append(ups, e) }, + Style: core.Style{Width: core.Pt(100), Height: core.Pt(30)}, + } + }, Environment(core.Environment{OS: core.OSWindows, Idiom: core.IdiomDesktop, KeyCodeNames: map[string]string{"KeyW": "z"}})) + app.Focus("id:pad") + app.KeyCodeDown("KeyW") + app.KeyDown("left") + if len(keys) != 2 || !slices.Contains(keys, "z") || !slices.Contains(keys, "left") || !slices.Equal(codes, []string{"KeyW"}) { + t.Fatalf("before blur: keys %v, codes %v; want left and z, KeyW", keys, codes) + } + app.BlurWindow() + if len(keys) != 0 || len(codes) != 0 { + t.Fatalf("after blur: keys %v, codes %v; want none", keys, codes) + } + physical, named := core.KeyEvent{Key: "z", Code: "KeyW"}, core.KeyEvent{Key: "left"} + if len(ups) != 2 || !slices.Contains(ups, physical) || !slices.Contains(ups, named) { + t.Fatalf("blur key-ups %+v, want %+v and %+v", ups, physical, named) + } +} + // TestKeysDownHearsKeysNoViewHears holds the keys a frame loop reads to follow KeyDown and KeyUp with no view that hears keys focused, once a read asked for every key, and to leave out those typed into a field (#1046). func TestKeysDownHearsKeysNoViewHears(t *testing.T) { var held string