• Advertisement
Sign in to follow this  

Mouse Movement In Windows

This topic is 4680 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

How do i get the change in X and Y of the mouse when it moves? I don't want the position of the cursor, because the cursor can reach the end of the screen and than it can't move no more...

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
When you trap the WM_ messages, the position is retrievable from lParam. Do something like this I guess...


// in your window proc handler...

static short int cursor_x_pos = 0;
static short int cursor_y_pos = 0;

static short int prev_cursor_x_pos = 0;
static short int prev_cursor_y_pos = 0;

if(WM_MOUSEMOVE == Msg)
{
prev_cursor_x_pos = cursor_x_pos;
prev_cursor_y_pos = cursor_y_pos;
cursor_x_pos = GET_X_LPARAM(lParam);
cursor_y_pos = GET_Y_LPARAM(lParam);

short int cursor_x_delta = cursor_x_pos - prev_cursor_x_pos;
short int cursor_y_delta = prev_cursor_y_pos - cursor_y_pos;

// do whatever you want with the deltas...
}


Share this post


Link to post
Share on other sites
Thanks, but as i said, i don't wan't to get the cursor's position, since i use the change in X and Y to rotate the charecter in a First Person Shooter. If i use the cursor, i can only move the mouse left till it reaches the left border of the screen, than it can no longer change it position.
I'm guessing that the mouse sends to windows a message with the distance it had move on each axis, and than windows moves the cursor by that amount, clipping it's position if it's past the borders of the screen...
any ideas?

Share this post


Link to post
Share on other sites
No_Germs,

You should read the AP's code a little bit closer. His code snippet isn't just getting the mous position, but its also keeping the previous mouse position and take a difference between them. If you are not wanting to show the mouse pointer, then you should make a call to 'ShowCursor' and you want to fix the position of the mouse to the center of the window, so you will also need to make a call to 'SetCursorPos' and set the x to 1/2 the width and the y to 1/2 the height.

-brad

Share this post


Link to post
Share on other sites
Oh, but i have read it carefuly :) it's identical to the method i'm using now.
as i said at the end of my previous post, the problem with this method is that when the cursor gets to the border of the screen, it's position can change no more, thus- the delta of it's position is zero. that means that my charecter can rotate only so much rounds in each direction...
that's why i want to get the delta of the mouse, not the delta of the cursor...

Share this post


Link to post
Share on other sites
Oh, sorry galapageous... i did read the AP message carefuly, but i can't say the same about your message... Sorry :O
the SetCursorPos was what i needed... Thanks :)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
For your copy+pastage pleasure:


void GetMouseDeltas (int &x, int &y)
{
if (!g_hwnd || GetFocus() != g_hwnd)
return;

// Get mouse position
POINT mousePos;
GetCursorPos(&mousePos);

// Get center of the window
POINT center = GetWindowCenter();

// Get delta
x = mousePos.x - center.x;
y = mousePos.y - center.y;

// Put mouse back to center of the window
SetCursorPos(center.x, center.y);
}



Should be called each frame ^_^.

Share this post


Link to post
Share on other sites
eh, that was me >_<.

btw:


POINT GetWindowCenter(void)
{
POINT ret = {0,0};
RECT wr;

GetWindowRect(g_hwnd, &wr);
ret.x = wr.left + (wr.right - wr.left) / 2;
ret.y = wr.top + (wr.bottom - wr.top) / 2;

return ret;
}

Share this post


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

  • Advertisement