WinAPI – Moving the mouse cursor without triggering WM_MOUSEMOVE

I want to use SetCursorPos to capture the mouse in the middle of the window (just like in an FPS game).

The problem is that when I do this, My window will receive WM_MOUSEMOVE-this will at most cancel any movement the user wants, and at worst it will enter a feedback loop.

I considered using GetCursorPos to read the position and ignore the message, if it does Same position as I moved it with SetCursorPos. But the problem with this method is that the mouse is asynchronous. If the program lags, then GetCursorPos will return a different value than I expected-so it won’t know to ignore the message.

Is there a good way to deal with this problem?

When the application has focus, the RawInput API should be used to complete such input and hide the cursor. This It means that it is not limited by the screen, and you don’t actually handle mouse events.

I want to use SetCursorPos to capture the mouse in the middle of the window (just like in an FPS game).

The problem is that when I do this, my window will receive WM_MOUSEMOVE-this will at most cancel any movement the user wants, and at worst it will enter the feedback loop.

< p>I considered using GetCursorPos to read the position and ignore the message, if it is the same as the position I moved it with SetCursorPos. But the problem with this method is that the mouse is asynchronous. If the program lags behind, then GetCursorPos will return one with me Expect different values-so it won’t know to ignore the message.

Is there a good way to deal with this problem?

When the application has focus, you should use the RawInput API to complete such input and hide the cursor. This means that it is not restricted by the screen, and you actually don’t Do not handle mouse events.

Leave a Comment

Your email address will not be published.