cannot receive WM_INPUT_DEVICE_CHANGE message after registered with RIDEV_DEVNOTIFY

Started by
8 comments, last by Frahm 10 years, 5 months ago

Hey, this is my first post, and I have some trouble when implementing my input framework for my game. I want to get notified whenever a keyboard is attached for detached from the computer, but I couldn't get it done.

Here is the relevant codes:


void Input::registerKeyboards() {
  RAWINPUTDEVICE rid[1];
  rid[0].usUsagePage = 0x01;
  rid[0].usUsage = 0x06;
  rid[0].dwFlags = RIDEV_DEVNOTIFY;
  rid[0].hwndTarget = 0;
  if(RegisterRawInputDevices(rid, 1, sizeof(RAWINPUTDEVICE)) == FALSE) {
    DebugTrace();
  }
}

and the message loop


  MSG msg;
  while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != 0) {
    switch(msg.message) {
      case WM_INPUT:
        input_.onKeyEvent(msg.lParam);
        break;
      case WM_INPUT_DEVICE_CHANGE:
        input_.onDeviceChanges(msg.wParam, msg.lParam);
        break;
      case WM_QUIT:
        this->exit_code_ = msg.wParam;
        return false;
      default:
        DispatchMessage(&msg);
    }
  }

I can receive all the WM_INPUT message, but no WM_INPUT_DEVICE_CHANGE.

There must be something I missed, any suggestions would be appreciated.

Advertisement

Why in heaven to you process messages in your message loop?

You really should only handle messages (beside WM_QUIT) in your window proc. DispatchMessage may actually create new messages while dispatching that will never even appear in your loop.

Second line on MSDN for WM_INPUT_DEVICE_CHANGE:

Sent to the window that registered to receive raw input.

A window receives this message through its WindowProc function.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Yes, you need to know on Windows there are 2 ways to take a message to you.

SendMessage directly calls the window procedure you gave on window creation when its used.

PostMessage just puts it in the message queue where you can retrieve it with GetMessage or PeekMessage, then you can feed it to TranslateMessage if you want WM_CHAR and similar messages for text input to be generated from keyboard messages and then you call DispatchMessage which checks what window the message belongs to and calls the window procedure for that window.

Thanks for answering my question.

Why in heaven to you process messages in your message loop?

You really should only handle messages (beside WM_QUIT) in your window proc. DispatchMessage may actually create new messages while dispatching that will never even appear in your loop.

Second line on MSDN for WM_INPUT_DEVICE_CHANGE:

Sent to the window that registered to receive raw input.

A window receives this message through its WindowProc function.

The reason that I process input messages inside the message loop is to avoid introducing another global variable. Yes, I have tried catching the WM_INPUT_DEVICE_CHANGE message inside the WndProc, and it failed. "Sent to the window that registered to receive raw input", does it mean that I must specify a single window handle to catching this message?


void Input::registerKeyboards(HWND handle) {
  RAWINPUTDEVICE rid[1];
  rid[0].usUsagePage = 0x01;
  rid[0].usUsage = 0x06;
  rid[0].dwFlags = RIDEV_DEVNOTIFY;
  rid[0].hwndTarget = handle;
  if(RegisterRawInputDevices(rid, 1, sizeof(RAWINPUTDEVICE)) == FALSE) {
    DebugTrace();
  }
}

this really works well, but I have more than one window, and I need the "inputs following the focus" feature. is there any way to work around this?

Yes, you need to know on Windows there are 2 ways to take a message to you.

SendMessage directly calls the window procedure you gave on window creation when its used.

PostMessage just puts it in the message queue where you can retrieve it with GetMessage or PeekMessage, then you can feed it to TranslateMessage if you want WM_CHAR and similar messages for text input to be generated from keyboard messages and then you call DispatchMessage which checks what window the message belongs to and calls the window procedure for that window.

Thanks, you mean that message might comes from a call to SendMessage, it sounds resonable, but after I tried this:


LRESULT CALLBACK WndProc(HWND handle,
                         UINT message,
                         WPARAM w_param,
                         LPARAM l_param) {
  if(message == WM_NCCREATE) {
    SetWindowLongPtr(handle, GWLP_USERDATA, 
    (LONG_PTR)(((LPCREATESTRUCT)l_param)->lpCreateParams));
    //let it propagate
  }
  
  if(message == WM_INPUT_DEVICE_CHANGE) {
    OutputDebugString(L"bingo\n");
  }

  //....

  return DefWindowProc(handle, message, w_param, l_param);
}

it didn't output "bingo" in my output window. I didn't call TranslateMessage since I don't want to process WM_CHAR.

Did you use the WndProcs HWND as the registered hwndTarget?

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Did you use the WndProcs HWND as the registered hwndTarget?

no, I set the hwndTarget as 0 since I have more than one window that needs keyboard inputs, and I don't want to restrict the inputs to one window.

/offtopic

Sorry for intruding, but I couldn't help noticing - how does this thread have over 14k views? blink.png

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

You could have a message only window that receives all raw input (using RIDEV_INPUTSINK) and passes it on to the other windows.

/offtopic

Sorry for intruding, but I couldn't help noticing - how does this thread have over 14k views? blink.png

Even me doubt about it, but I found that this thread has good rankings when searching with these keywords: "WM_INPUT_DEVICE_CHANGE", "RIDEV_DEVNOTIFY".

You could have a message only window that receives all raw input (using RIDEV_INPUTSINK) and passes it on to the other windows.

nice, it works.

Thank you all, guys.

This topic is closed to new replies.

Advertisement