简体   繁体   中英

How to get mouse movement when cursor is locked in screen

I have an application (game) where I need cursor to be hidden, since the player is supposed to control an entity with a mouse. I also had to lock cursor (set middle of screen position using SetCursorPos in mousemove event handler).

Is there a viable and easy solution to tracking mouse movements?

Right now I have a dirty hack of bool value which allows me to ignore next event handler right after a SetCursorPos method is called. But I don't like it and it doesn't work properly - mouse moves are not precise.

Alas, I cannot use DirectInput in this project.

I have used GetRawInputData instead of mouse screen coordinates. It worked like a charm.

In the event handler

LRESULT CALLBACK Application::StaticWndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
...
case WM_INPUT: 
{
    UINT dwSize = 40;
    static BYTE lpb[40];

    GetRawInputData((HRAWINPUT)lParam, RID_INPUT, 
                    lpb, &dwSize, sizeof(RAWINPUTHEADER));

    RAWINPUT* raw = (RAWINPUT*)lpb;

    if (raw->header.dwType == RIM_TYPEMOUSE) 
    {
        int xPosRelative = raw->data.mouse.lLastX;
        int yPosRelative = raw->data.mouse.lLastY;
        GameState::Instance()->MoveCursor(xPosRelative, yPosRelative);
        RECT rect;
        //resetting cursor to stay in center
        if(GetWindowRect(Application::Instance()->m_hWnd, &rect))
            SetCursorPos((rect.right - rect.left) / 2, (rect.bottom - rect.top) / 2);
    } 
    break;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM