Jump to content
  • Advertisement
Sign in to follow this  
ekba89

raw input mouse problem

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

I created a simple input system and it works mostly. Right now I have a camera that is moved with the mouse when left button is clicked. The problem I am having is when mouse button is clicked even when I stop moving the mouse lastX or lastY doesnt become 0 and camera keeps moving until I stop clicking the button. Here is my code

//InputManager::HandleInput() function

UINT dwSize;

GetRawInputData(rawInput, RID_INPUT, 0, &dwSize, sizeof(RAWINPUTHEADER));

LPBYTE lpb = new BYTE[40];
	
if(GetRawInputData(rawInput, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER)) != dwSize)
{
	OutputDebugString("Cannot get raw input data!");
}

if(raw->header.dwType == RIM_TYPEMOUSE)
    {
        mouseInfo.movementX = raw->data.mouse.lLastX;
        mouseInfo.movementY = raw->data.mouse.lLastY;
        //printf("mouse movement x=%d, y=%d\n", raw->data.mouse.lLastX, raw->data.mouse.lLastY);

        bool downState = (raw->data.mouse.usButtonFlags & RI_MOUSE_LEFT_BUTTON_DOWN) > 0;
        bool upState = (raw->data.mouse.usButtonFlags & RI_MOUSE_LEFT_BUTTON_UP) > 0;

        if(downState == true && upState == false)
        {
            mouseInfo.leftButton = true;
        }
        if(upState == true)
        {
            mouseInfo.leftButton = false;
        }

        downState = (raw->data.mouse.usButtonFlags & RI_MOUSE_RIGHT_BUTTON_DOWN) > 0;
        upState = (raw->data.mouse.usButtonFlags & RI_MOUSE_RIGHT_BUTTON_UP) > 0;

        if(downState == true && upState == false)
        {
            mouseInfo.rightButton = true;
        }
        if(upState == true)
        {
            mouseInfo.rightButton = false;
        }
    }

Share this post


Link to post
Share on other sites
Advertisement

According to MSDN lastX and lastY value are relative to the last position. So they are not screen coordinates and they become 0 if you dont move the mouse. Also as I said as soon as I stop take of my finger from the left mouse button values become 0 again. My problem is they are not getting updated when mouse button is clicked.

Share this post


Link to post
Share on other sites

lLastX

Type: LONG

The motion in the X direction. This is signed relative motion or absolute motion, depending on the value of usFlags.

lLastY

Type: LONG

The motion in the Y direction. This is signed relative motion or absolute motion, depending on the value of usFlags.

 

That, plus when the button is pressed & released, you can assume that no mouse movement happens, so if either of the RI_MOUSE_RIGHT_BUTTON_DOWN and RI_MOUSE_RIGHT_BUTTON_UP change state, then you can just use a movement of (0, 0).

 

You culd also try ZeroMemory'ing your RAWINPUT structure to all zeroes before every call to GetRawInputData because, maybe when there's no mouse movement, GetRawInputData leaves the lLastX and lLastY values unmodified since your last call. But using a movement of (0, 0) when either of the button flags change state should be faster than ZeroMemory'ing the RAWINPUT structure.

Edited by tonemgub

Share this post


Link to post
Share on other sites

Mine never gets absolute motion. I have tried putting it inside if statement to see what I get and it is always relative. And I tried printing it out too and I get values between -3 and 3.

Share this post


Link to post
Share on other sites

Ok I will share what I have found so far. If I move my mouse 1 pixel in any direction while my left button is clicked it stays that way. So if I move in x+ direction x becomes 1 (-1 for x- and so on) and stays that way until I move my mouse again or I release mouse button.

 


That, plus when the button is pressed & released, you can assume that no mouse movement happens, so if either of the RI_MOUSE_RIGHT_BUTTON_DOWN and RI_MOUSE_RIGHT_BUTTON_UP change state, then you can just use a movement of (0, 0).

 

Setting movement when it is clicked doesnt work because as I found now it happens after the click.

Share this post


Link to post
Share on other sites

You don't mention WM_INPUT message processing. Are you using GetRawInputData() in response to WM_INPUT, or using it for polling? All the info I could find describes that function only as a response to WM_INPUT, not as a means for polling. As Vortez mentions, if the mouse isn't moving, the data won't change. If you're not getting a WM_INPUT message, it appears you might be getting the last state of the mouse, not the current state. You don't get x=y=0 until other data changes - i.e., when the mouse button is released. GetInputRawData may not have a defined behavior if you haven't received WM_INPUT.

 

Reiterating and expanding on Vortez' and L. Spiro's comments - don't ask for data until it changes as indicated by a WM_INPUT message.

 

About Raw Input

Edited by Buckeye

Share this post


Link to post
Share on other sites

You don't mention WM_INPUT message processing. Are you using GetRawInputData() in response to WM_INPUT, or using it for polling? All the info I could find describes that function only as a response to WM_INPUT, not as a means for polling. As Vortez mentions, if the mouse isn't moving, the data won't change. If you're not getting a WM_INPUT message, it appears you might be getting the last state of the mouse, not the current state. You don't get x=y=0 until other data changes - i.e., when the mouse button is released. GetInputRawData may not have a defined behavior if you haven't received WM_INPUT.

 

Reiterating and expanding on Vortez' and L. Spiro's comments - don't ask for data until it changes as indicated by a WM_INPUT message.

 

About Raw Input

I was using when WM_INPUT happens not for polling. I solved the problem by adding a update function which resets values for mouse movement.

 

Also another problem I am having is that I have a delay and it is not that smooth when I move mouse quickly. My scene only have a box and a plane right now so my fps is good enough and if I use vsync this problem gets worse. So I started wondering is RawInput a good way to take inputs or am I the only one having problems with it? I am basically using the example code in MSDN page. I just wrapped it into my input manager class.

Share this post


Link to post
Share on other sites

Personnally i prefer doing this by responding to WM_MOUSExxxx messages, it keep thing simple and it's the way windows work, you only get notified when something happen.

I think rawinput is better for pooling.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!