Jump to content
  • Advertisement
Sign in to follow this  
SteveDeFacto

Locking cursor within WinProc.

This topic is 2533 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to allow my program to hide the cursor and lock its position to get relative inputs for rotating a camera or for anything else that needs relative mouse position. When I set the position of the mouse back to the center of the window it causes a feedback loop because setting the mouse position also sends a mouse move message to the window. My solution was to peek the message after moving the cursor but this also clears keyboard messages. What should I do?


// On_MouseMove is a callback
case WM_MOUSEMOVE:
{
if(Window->On_MouseMove)
{
if(Window->lockmouse)
{
RECT WindowRect;
GetWindowRect( Window->hWnd, &WindowRect );
long cx = WindowRect.left + ((WindowRect.right - WindowRect.left) / 2);
long cy = WindowRect.top + ((WindowRect.bottom - WindowRect.top) / 2);
POINT Point;
GetCursorPos( &Point );
Window->On_MouseMove( Point.x - cx, Point.y - cy );
SetCursorPos( cx, cy );
PeekMessage( NULL, Window->hWnd, 0, 0, PM_REMOVE );
}
else
{
Window->On_MouseMove( LOWORD(lParam), HIWORD(lParam) );
}
}
break;
}


Also I tried this:
PeekMessage( NULL, Window->hWnd, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE );
However the camera motion became choppy when using both the mouse and keyboard at the same time. I figured by using WM_MOUSEFIRST and WM_MOUSELAST I would only clear the mouse messages.

Share this post


Link to post
Share on other sites
Advertisement
Well, the mouse gets sent to the center, so it obviously is a move. You could however only call SetCursorPos, if the current mouse pos is different from the center.

Also, your center position is centering on the window (plus frame), not the client. Consider using GetClientRect.

And while you're at it, for this kind of movement ClipCursor is also advisable, to keep the mouse inside your window. If you do that you should also handle focus loss and disable the cliprect properly.

Share this post


Link to post
Share on other sites

Well, the mouse gets sent to the center, so it obviously is a move. You could however only call SetCursorPos, if the current mouse pos is different from the center.

Also, your center position is centering on the window (plus frame), not the client. Consider using GetClientRect.

And while you're at it, for this kind of movement ClipCursor is also advisable, to keep the mouse inside your window. If you do that you should also handle focus loss and disable the cliprect properly.


Well I think I fixed my main problem by dropping the whole routine into my main loop bypassing WM_MOUSEMOVE which was causing the choppy motion. Also I am using ClipCursor however I can't see how I can use GetClientRect for this without knowing the top and left of the ClientRect to get a center relative to the screen instead of the window which is needed for SetCursorPos.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!