none
WM_INPUT

    Question

  • What is the correct method to handle WM_INPUT messages for Mouse and Keyboard input captures?  My code is as follows:

    After registering in the WM_INPUT msg of WndProc

    I call GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER));

    LPBYTE lpb = new BYTE[dwSize];

    raw = (RAWINPUT*)lpb;

    uiManager->Update(raw);

    moving to Update(raw):

    if(raw->header.dwType == RIM_TYPEMOUSE)
    {
    // Read Mouse Data
    mx = raw->data.mouse.lLastX;
    my = raw->data.mouse.lLastY;

    }
    if(raw->header.dwType == RIM_TYPEKEYBOARD)
    {
    // Get key value from the keyboard member (of type RAWKEYBOARD)
    keyCode = raw->data.keyboard.VKey;
    switch(keyCode)
    {
    case VK_"Apple-tab-span" style="white-space:pre;">// move player left
    break;
    case VK_RIGHT:
    // move player right etc.
    break;
    case VK_ESCAPE:
    // Exit Game if escape is pressed
    MessageBox(NULL, L"Escape Key was pressed", L"MSG", MB_OK);
    break;
    }

    The message box fires when I hit the escape key however I can't click the OK button on it after it opens, I must end the debugging session in VC++ 2010.

    Is there a correct way to handle these messages and have the application not lock up?

    Saturday, March 17, 2012 5:31 PM

Answers

  • I'm not very familiar with the Raw Input aspect knowledge, I just used a simple Windows Form application demonstrate the capture ESC key down message, and then show a MessageBox, I can move this MessageBox to anywhere and close it as usual.

            int WM_KEYDOWN = 0x0100;
            const Int32 VK_ESCAPE = 0x1B;
            const Int32 Z = 0x5A;
            protected override void WndProc(ref Message m)
            {
                if (m.Msg == WM_KEYDOWN)
                {
                    switch (m.WParam.ToInt32())
                    {
                        case VK_ESCAPE:
                            MessageBox.Show("", "", MessageBoxButtons.OKCancel);
                            break;
                        case Z:
                            this.Text = "Z";
                            break;
                        default:
                            break;
                    }
                }
                base.WndProc(ref m);
            }

     Maybe you can use the console print out the strings to replace the MessageBox first for debugging.(And  you also can use the breakpoint at this code line.) And print out the call times, sometimes the problem in the WndProc function is caused by some repeated message passing and effect our job.

    I saw that this aspect maybe suitable in the UI dev aspect forum,

    WM_INPUT http://msdn.microsoft.com/en-us/library/windows/desktop/ms645590(v=vs.85).aspx

    Best wishes,


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us


    Tuesday, March 20, 2012 7:37 AM
  • I have decided against using the WM_INPUT message as there are not any complete examples or articles on how to implement this successfully.  I am using WM_CHAR, WM_KEYDOWN, and WM_MOUSE messages for my game.

    Thanks

    Thursday, March 22, 2012 3:22 PM

All replies

  • Since MessageBox is reached only when you press <Esc>, then your handler is likely correct. But probably you cannot execute MessageBox here, therefore comment it if you needed it only for checks. Does it work after removing MessageBox?


    Saturday, March 17, 2012 6:28 PM
  • Yes, it works without the message box, no legacy messages are sent to the App from the Mouse or Keyboard and I can see Mouse messages and Keyboard key strokes in the debugger and the print window of the debugger.  My issue is how to capture key presses and mouse clicks and do something with them while allowing the game to continue running.

    Saturday, March 17, 2012 6:31 PM
  • I think that you can post a custom message (defined with RegisterWindowMessage or WM_APP+X) to one of your windows when you detect a particular condition, and then handle this message.


    Saturday, March 17, 2012 6:38 PM
  • I am capturing the messages ok I think, its leaving the loop when I perform an action that seems to be the problem.  Do I need to call DefWindowProc() or something like that to ensure the app doesn't hang?

    Also, what is the benefit of using the custom messages you mention?

    Saturday, March 17, 2012 6:42 PM
  • I have implemented working code that captures mouse and keyboard messages however the app hangs and I get an hourglass with the mouse cursor.  Via debugging, I can still see the messages captured however I can't move the window by the title bar or maximize it or anything else.

    Any ideas?

    Sunday, March 18, 2012 6:26 PM
  • Try the WDK forum. Some folks there definitely know how to handle raw input.

    -- pa

    Sunday, March 18, 2012 7:58 PM
  • Hi Sosa,

    According to your description and Pavel's suggestion, I'd like to move this thread to "Windows Hardware WDK and Driver Development Forum" for better support.

    Thanks for your understanding and active participation in the MSDN Forum.
    Helen Zhao


    Helen Zhao [MSFT]
    MSDN Community Support | Feedback to us

    Monday, March 19, 2012 7:06 AM
  • this is a user mode issue, nobody here understands this level of interaction . please move the thread back to the original forum

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Monday, March 19, 2012 4:08 PM
  • What do you mean?  That this forum is not for understanding the inner workings of how WM_INPUT and windows messaging works?
    Monday, March 19, 2012 4:12 PM
  • the WDK forum is for kernel mode driver development, windows messages such as WM_INPUT are user mode abstractions that drivers are not involved with. the original forums is more suited to answering the question

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Monday, March 19, 2012 8:21 PM
  • Thank you, appreciate it.
    Monday, March 19, 2012 8:47 PM
  • I'm not very familiar with the Raw Input aspect knowledge, I just used a simple Windows Form application demonstrate the capture ESC key down message, and then show a MessageBox, I can move this MessageBox to anywhere and close it as usual.

            int WM_KEYDOWN = 0x0100;
            const Int32 VK_ESCAPE = 0x1B;
            const Int32 Z = 0x5A;
            protected override void WndProc(ref Message m)
            {
                if (m.Msg == WM_KEYDOWN)
                {
                    switch (m.WParam.ToInt32())
                    {
                        case VK_ESCAPE:
                            MessageBox.Show("", "", MessageBoxButtons.OKCancel);
                            break;
                        case Z:
                            this.Text = "Z";
                            break;
                        default:
                            break;
                    }
                }
                base.WndProc(ref m);
            }

     Maybe you can use the console print out the strings to replace the MessageBox first for debugging.(And  you also can use the breakpoint at this code line.) And print out the call times, sometimes the problem in the WndProc function is caused by some repeated message passing and effect our job.

    I saw that this aspect maybe suitable in the UI dev aspect forum,

    WM_INPUT http://msdn.microsoft.com/en-us/library/windows/desktop/ms645590(v=vs.85).aspx

    Best wishes,


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us


    Tuesday, March 20, 2012 7:37 AM
  • I am writing to check the status of the issue on your side.

    What about this problem now?

    Would you mind letting us know the result of the suggestions?


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us

    Thursday, March 22, 2012 7:06 AM
  • I have decided against using the WM_INPUT message as there are not any complete examples or articles on how to implement this successfully.  I am using WM_CHAR, WM_KEYDOWN, and WM_MOUSE messages for my game.

    Thanks

    Thursday, March 22, 2012 3:22 PM
  • You're welcome!

    Have a good weekend!


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us

    Friday, March 23, 2012 9:11 AM