package windows import ( "syscall" "testing" "time" ) // TestHookReceivesMouseMoveIntegration 真实移动鼠标验证钩子事件流。 // 仅在显式启用时运行(默认跳过,避免干扰用户会话)。 func TestHookReceivesMouseMoveIntegration(t *testing.T) { if testing.Short() { t.Skip("short mode") } h := NewMouseHook(64) if err := h.Start(); err != nil { t.Fatal(err) } defer h.Stop() time.Sleep(300 * time.Millisecond) if err := h.InstallErr(); err != nil { t.Fatalf("install: %v", err) } // 移动到两个不同位置,确保产生真实移动事件。 procSetCursorPos := syscall.NewLazyDLL("user32.dll").NewProc("SetCursorPos") moved := false for _, pos := range [][2]int32{{100, 100}, {400, 300}} { r, _, _ := procSetCursorPos.Call(uintptr(pos[0]), uintptr(pos[1])) if r != 0 { moved = true } time.Sleep(100 * time.Millisecond) } if !moved { t.Skip("session has no active input desktop (e.g. disconnected RDP); physical input unavailable") } select { case ev := <-h.Events(): t.Logf("received mouse event: msg=%#x pos=(%d,%d)", ev.Message, ev.X, ev.Y) case <-time.After(2 * time.Second): // 区分环境问题(回调从未被调用)与代码问题(回调被调用但事件丢失)。 if h.CallbackCalls() == 0 { // SetCursorPos 成功但系统从未回调钩子:无活动输入桌面。 procForeground := syscall.NewLazyDLL("user32.dll").NewProc("GetForegroundWindow") fg, _, _ := procForeground.Call() t.Logf("callback calls = 0, foreground=%x", fg) t.Skip("session lacks active input delivery (RDP/headless)") } t.Fatalf("callback called %d times but no event received", h.CallbackCalls()) } }