6

I want to trap the mouse in the middle of the window (like in an FPS game) using SetCursorPos.

The problem is that when I do, my window receives a WM_MOUSEMOVE -- which at best cancels out any movement that the user intended, and at worst enters a feedback loop.

I've considered reading the position using GetCursorPos and ignoring the message if it's the same as where I'd moved it with SetCursorPos. The problem with this approach though is that the mouse is asynchronous. If the program ever gets behind, then GetCursorPos will return a value different from what I'd expected -- and so it won't know to ignore the message.

Is there a good way to deal with this problem?

Rei Miyasaka
  • 7,007
  • 6
  • 42
  • 69
  • You don't need to call `GetCursorPos`, the location of the movement that caused the `WM_MOUSEMOVE` to be post is already in the `lParam`. Then you won't have the 'asynchronous' problem. – Sertac Akyuz May 13 '12 at 22:20
  • @SertacAkyuz The problem with that is that then you'd still need to rely on the window position to determine the mouse position relative to the screen, and the window position can change while the program's locked up doing something else. – Rei Miyasaka May 13 '12 at 22:41

1 Answers1

4

This sort of input should be done with the RawInput API and the cursor hidden when you app has focus. This means it is not bounded by the screen and you don't actually deal with the mouse events.

Deanna
  • 23,876
  • 7
  • 71
  • 156