Mouse Movement In Windows

Started by
6 comments, last by cow_in_the_well 19 years ago
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...
Advertisement
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...}
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?
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
-brad
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...
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 :)
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 ^_^.
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;}

- Thomas Cowellwebsite | journal | engine video

This topic is closed to new replies.

Advertisement