[C++] Getting the mouse data

Junior Spellweaver
Joined
Jan 15, 2011
Messages
150
Reaction score
8
I've been working on a game lately using DirectX, and I want to add some mouse controls in it.
First, I tried to get the mouse data using GetCursorPos (I only need the position, not the buttons).
Here's the function:

Code:
POINT GetMousePos(HWND hWnd)
{
	POINT cursorPos;
	GetCursorPos(&cursorPos);
	ScreenToClient(hWnd, &cursorPos);
	return cursorPos;
}

This made a really weird bug in the game.
The mouse position was correct at the top left of the screen, but going down with the mouse creates a gap between the mouse and the given coordinates.
The more I move to the right and bottom, the bigger the gap gets.
For example, when my mouse is at 5,5 it displays 5,5.
When my mouse is at 200,200 it displays 205,210
When my mouse is at 500,500 it displays 510,520
(These is not the data as it displays, but it comes really close)
Note that the Y is growing faster than the X.

After this I tried using the WM_MOUSEMOVE method, but this gave the same result.
I'm only using 1 monitor and the size of the window is 1024x768 (window mode).
Can someone help me out?
 
Use DirectInput or windows messages.

I already tried windows messages, the WM_MOUSEMOVE method.
And I don't understand DirectInput, it doesn't give the absolute mouse position, so it should update a point variable every frame.
But how can I get the starting position of the mouse when the other methods are giving wrong coordinates?
 
I read them all but I didn't see anything about the mouse position D:
Only the keyboard, mouse buttons and scroll wheel are explained.
 
DirectInput is a bad idea... Just sayin...
You tried WM_MOUSEMOVE and........
This section provides information about the Microsoft DirectInput component of the Microsoft DirectX application programming interface (API). The DirectInput API is used to process data from a joystick, or other game controller. The use of DirectInput for keyboard and mouse input is not recommended, Windows messages should be used instead (see Taking Advantage of High-Definition Mouse Movement for additional information). Note that the XInput API has been introduced for Xbox 360 controller support on Windows, see XInput and DirectInput for additional information.
 
Last edited:
Code:
#include <dinput.h>
LPDIRECTINPUT8 din;		// pointer to the DirectInput interface
LPDIRECTINPUTDEVICE8 dmouse;	// pointer to the mouse device
DIMOUSESTATE mousestate;	// pointer t about the mouse information

void init_mouse(HINSTANCE hInstance,HWND hWnd){
    // create a direct input interface
    DirectInput8Create(hInstance,              // Handle to the application
                       DIRECTINPUT_VERSION,    // compatible version
                       IID_IDirectInput8,      // DirectInput interface version
                       (void**)&din,           // pointer to the interface
                       NULL);                  // COM, leave it NULL

     //create device
     din->CreateDevice(GUID_SysMouse,
                      &dmouse,
                      NULL);
     //set format
     dmouse->SetDataFormat(&c_dfDIMouse);
     
     //set control you will have over the device
     dmouse->SetCooperativeLevel(hWnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND);
}


void detect_mouse(){
        //access the the input if you dnt already have access
        dmouse->Acquire();

	//access mouse information
	dmouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mousestate);
}

void clean_mouse(){
      dmouse->Unacquire();      // close mouse
      din->Release();		// close directinput
}

The mousestate holds all the information about the mouse:
Code:
mousestate.lX;           //movement on the X axis
mousestate.lY;           //movement on the Y axis
mousestate.lZ;           //movement of the mouse Wheel
mousestate.rgbButtons[4];//the mouse buttons, 0 - left, 1 - right,2 - middle, 3 - back, 4 - forward

The starting position of the mouse is usually in the center of the window
 
Last edited:
@xPain: The windows messages are giving me the same wrong coordinates as the GetCursorPos() does.

@GHOST107: The mousestate only gives the movement of the mouse, not the absolute position.
I could use this, but then I still need a starting position to start calculating, and that won't work without the other methods.
 
The coordinates are still wrong. :/
And I tested all methods on other computers and other mouses, but all give the same results.
I made a texture drawing in the middle of the mouse.
Here are my current codes:
Code:
if(message.message == WM_MOUSEMOVE)
{
	MousePosition = MAKEPOINTS(message.lParam);
}
Code:
position[21].x = MousePosition.x - 16; //Texture width: 32
position[21].y = MousePosition.y - 16; //Texture height: 32

And here's a picture of what happens:
VisualChaser - [C++] Getting the mouse data - RaGEZONE Forums


and the only warning I get is:
Code:
warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
But I don't think this should be a problem because the other methods aren't giving any warnings and get the same results.
 
Back