Microsoft không khuyến nghị DirectInput cho đầu vào bàn phím và chuột. Như vậy, tôi đã viết một lớp quản lý đầu vào có sử dụng SetWindowsHookEx để nối vào WndProc và GetMsg. Tôi tin rằng các móc được đặt một cách thích hợp, mặc dù chúng có vẻ là nguyên nhân của nhiều vấn đề khác nhau.Tại sao Windows không nhận được một số tin nhắn nhất định?
Cả WndProc lẫn GetMsg của tôi đều không nhận được bất kỳ thông báo nào mà WndProc thực nhận được. Trình quản lý đầu vào của tôi không bao giờ nhận được các tin nhắn WM_INPUT, WM_ BUTTON, WM_MOUSEWHEEL và WM_KEY * mà bạn cần.
Điều gì mang lại?
tiêu đề từng phần:
namespace InputManager
{
class CInputManager
{
HWND m_Window;
HHOOK m_WndProcHook;
HHOOK m_GetMessageHook;
static LRESULT CALLBACK WindowsProcedureHookProcedure(int Code, WPARAM WParameter, LPARAM LParameter);
static LRESULT CALLBACK GetMessageHookProcedure(int Code, WPARAM WParameter, LPARAM LParameter);
static LRESULT CALLBACK MessageHandler(HWND Window, UINT Message, WPARAM wParameter, LPARAM lParameter);
};
}
nguồn từng phần:
namespace InputManager
{
bool CInputManager::Initialize(HWND Window)
{
m_Window = Window;
// Hook into the sent messages of the target window to intercept input messages.
m_WndProcHook = SetWindowsHookEx(WH_CALLWNDPROC, &(WindowsProcedureHookProcedure), NULL, GetCurrentThreadId());
// Hook into the posted messages of the target window to intercept input messages.
m_GetMessageHook = SetWindowsHookEx(WH_GETMESSAGE, &(GetMessageHookProcedure), NULL, GetCurrentThreadId());
// Register mouse device for raw input.
RAWINPUTDEVICE RawInputDevice;
RawInputDevice.usUsagePage = HID_USAGE_PAGE_GENERIC;
RawInputDevice.usUsage = HID_USAGE_GENERIC_MOUSE;
RawInputDevice.dwFlags = RIDEV_INPUTSINK;
RawInputDevice.hwndTarget = m_Window;
return RegisterRawInputDevices(&(RawInputDevice), 1, sizeof(RawInputDevice));
}
void CInputManager::Shutdown()
{
// Unhook from the posted messages of the target window.
UnhookWindowsHookEx(m_GetMessageHook);
// Unhook from the sent messages of the target window.
UnhookWindowsHookEx(m_WndProcHook);
}
LRESULT CALLBACK CInputManager::WindowsProcedureHookProcedure(int nCode, WPARAM wParameter, LPARAM lParameter)
{
if(nCode == HC_ACTION)
{
// Forward to message handler.
CWPSTRUCT* Message = reinterpret_cast<CWPSTRUCT*>(lParameter);
MessageHandler(Message->hwnd, Message->message, Message->wParam, Message->lParam);
}
return CallNextHookEx(NULL, nCode, wParameter, lParameter);
}
LRESULT CALLBACK CInputManager::GetMessageHookProcedure(int nCode, WPARAM wParameter, LPARAM lParameter)
{
if(nCode == HC_ACTION)
{
// Forward to message handler.
CWPSTRUCT* Message = reinterpret_cast<CWPSTRUCT*>(lParameter);
MessageHandler(Message->hwnd, Message->message, Message->wParam, Message->lParam);
}
return CallNextHookEx(NULL, nCode, wParameter, lParameter);
}
}
Tôi không bao gồm mã cho xử lý tin nhắn như nó bao gồm 149 dòng, hầu hết trong số đó là công tắc cho loại tin nhắn. Các giá trị tin nhắn nhận được trong WndProc không giống như các giá trị trong callback của tôi.
Cửa sổ mục tiêu có nằm trong cùng một quy trình không? –
Vâng, đúng vậy. Mọi thứ đều ở dạng nhị phân đơn. –