Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move mouse cursor without triggering a WM_MOUSEMOVE

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?

like image 773
Rei Miyasaka Avatar asked May 13 '12 21:05

Rei Miyasaka


Video Answer


1 Answers

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.

like image 113
Deanna Avatar answered Oct 28 '22 23:10

Deanna