3

I have recently started reading Beginning DirectX 11 Programming(Allen Sherrod, Wendy Jones) and have stumbled upon a problem concerning input. The book only teaches me how to use Win32, DirectInput and XInput for input-handling. After a bit of research, however, I have realized that I should be using RawInput for input handling. This is where the problem arises.

I have managed to enable my application to receive raw mouse input. My question to you guys is: how do I interpret the raw mouse data and use it in my game, similar to how you use WM_MOUSEMOVE?

Edit: Sorry for formulating myself badly. I want to know where the mouse pointer is located within the screen of my application but don't understand the values of the mouse's raw input. (mX, mY)

    case WM_INPUT:
    {
        UINT bufferSize;
        GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &bufferSize, sizeof(RAWINPUTHEADER));
        BYTE *buffer = new BYTE[bufferSize];
        GetRawInputData((HRAWINPUT)lParam, RID_INPUT, (LPVOID)buffer, &bufferSize, sizeof(RAWINPUTHEADER));

        RAWINPUT *raw = (RAWINPUT*) buffer;

        if ( raw->header.dwType == RIM_TYPEMOUSE)
        {
            long mX = raw->data.mouse.lLastX;
            long mY = raw->data.mouse.lLastY;
        }
     }
Lucas Perrera
  • 31
  • 1
  • 4
  • Win32 is a much wider area than you think. Perhaps you meant it teaches you window messages. – chris Jul 05 '13 at 20:55
  • This is a very broad and generic question. Strictly speaking, you use it by calling the related API functions. – DanielKO Jul 05 '13 at 21:00
  • You have a memory leak every time mouse input is received. That's not something you want. Use a vector. – chris Jul 05 '13 at 21:10

1 Answers1

2

You could achieve this by doing it like this:

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;
    } 
    break;
}

As mentioned in Mouse movement with WM_INPUT(Article applies to non-high definition aswell). The article contains an example of WM_MOUSEMOVE aswell.

Floris Velleman
  • 4,848
  • 4
  • 29
  • 46
  • 2
    Floris Velleman, it is important to note here that "dwSize" value will not allways be 40! (I.e. for a 64bit application, the value will be 48) – Gediminas Dec 11 '16 at 16:40