check for mouse movement

Started by
9 comments, last by iedoc 16 years, 2 months ago
can anybody help me out with my mouse problem? how do i detect mouse movement in my code?
Advertisement
MSDN docs
i know that... but thanks. i already checked it actually, theres just so much, i was hoping for something a little more simple. like when you check for keyboard input, and mouse button input. i just want it to check for mouse movement. true or false type of thing.
Well are you using windows messages for handling your keyboad? If so do you wish to use this method also for mouse movement. Additionaly if you are using direct input, then please say. I am not sure how to respond to this question without more information.

Either way both methods are fairly simple, and straight foward. I am sure you can be helped if you elaborate more. Thank you.
sorry, yeah, i'm using windows messages to check for input. i'd like to use it for the mouse to, and i don't know much about directinput yet, but i would way rather use that for the mouse if its simple enough to set up
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>

class TheMouse {

LPDIRECTINPUT8 pdi;
LPDIRECTINPUTDEVICE8 pmouse;

public:

TheMouse(HWND hwnd)
{
DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (LPVOID*)&pdi, NULL);

pdi->CreateDevice(GUID_SysMouse, &pmouse, NULL);
pmouse->SetDataFormat(&c_dfDIMouse);
pmouse->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
pmouse->Acquire();
}

void getstate(int *dx, int *dy)
{
DIMOUSESTATE m_state;
pmouse->GetDeviceState(sizeof(DIMOUSESTATE), &m_state);
*dx = m_state.lX;
*dy = m_state.lY;
}

~TheMouse()
{
if (!pmouse) return;
pmouse->Unacquire();
pmouse->Release();
}


};


ok, to initialize, i need to call TheMouse::TheMouse(hwnd)
but to get the coords, i can't figure out how to get them out of the getstate. i know i need to do something with TheMouse::getstate(something, something) i think, can anybody help me pull out the coords from this?
You don't want to use DirectInput for mouse input. Please, just take my word for it. In fact this topic has been discussed here so many times I wish there were just a sticky I could link to.

At any rate...on WinXP and above all DirectInput does internally is register the mouse or keyboard as a Raw Input device and start a thread that reads WM_INPUT messages. This is kind of a waste, since a separate thread is really overkill for handling a few messages. If you want the high-precision mouse input that Raw Input affords you, you're better off just handling it yourself. The other benefit of handling it yourself is that you can also receive legacy WM_MOUSEMOVE messages, which is what you'll want to use for any GUI's you might have in your app (since these messages have pointer ballistics applied, which will make the cursor movement feel much more natural to the user).

As for the keyboard, there's really no reason to use Raw Input. With standard windows messages you'll retain support for alternate character maps and international keyboards, and you'll also retain normal usage of caps lock, shift, and key repeats.

Of course if you don't want to take my word for it, you can read what Microsoft has to say about it.

P.S. In case you didn't catch it, WM_MOUSEMOVE is the message you'll want to process for your situation. You'll receive one of those whenever the mouse moves inside your window's client area.
here is the deal with direct input. It does not provide you with mouse X and Y positions/coords. What you have to do is get the current mouse position when you first start direct input and then in the direction it moves you add/subtract from the X and Y position/coords. Thats it.
Quote:Original post by iedoc
ok, to initialize, i need to call TheMouse::TheMouse(hwnd)
but to get the coords, i can't figure out how to get them out of the getstate. i know i need to do something with TheMouse::getstate(something, something) i think, can anybody help me pull out the coords from this?
Reasons not to use DirectInput for kayboard or mouse input. And you can't get mouse coordinates. That's one of the many limitations of DirectInput.

As for the OP: The easiest way would be to check WM_MOUSEMOVE messages. If you get one, the mouse moved.
Of course you cant get the mouse position using direct input. I never said that. I said you get the mouse position before you start direct input. Meaning you need to check the windows message first and then you can use direct input for your mouse position if you use the method I described. But you wouldnt do this anyways, because you would already have set-up a mouse movement/position function previously. Unless you wanted to use direct input for some reason instead of windows messages.

Thank you.

This topic is closed to new replies.

Advertisement