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.