# DIRECTINPUT: Finding Absolute Mouse Coordinates....

## Recommended Posts

fpsgamer    856
I am having trouble resolving the absolute screen position of the cursor. Here is my setup of the DirectInput mouse device...it is currently set up in relative mode (only because I'm not sure how to set it up in absolute mode):
if (lpdi->CreateDevice(GUID_SysMouse, &lpdimouse, NULL)!=DI_OK)
return(0);

// set cooperation level
if (lpdimouse->SetCooperativeLevel(main_window_handle,
DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)!=DI_OK)
return(0);

// set data format
if (lpdimouse->SetDataFormat(&c_dfDIMouse)!=DI_OK)
return(0);

// acquire the mouse
if (lpdimouse->Acquire()!=DI_OK)
return(0);


Then in my main event loop I poll the mouse and try to convert the relative coordinates to absolute coordinates:
mouse_x += mouse_state.lX; //initialized to screen_width/2;
mouse_y += mouse_state.lY; //initialized to screen_height/2;


I thought that would give me absolute cordinates but the values do not seem to be correct. For example, if i drag the cursor to the corner of the screen the position doesnt appear as (0,0). How can I easily obtain the absolute (x,y) position of the cursor?

##### Share on other sites
bpoint    464
There is no direct way to get the absolute position of the mouse in DirectInput. You might want to try GetCursorPos instead.

Once you get the point from GetCursorPos, you need to shift it relative to the client area of your window (see GetWindowInfo), then clamp it to the size of your window.

## Create an account

Register a new account