Microsoft does not recommend DirectInput for keyboard and mouse input. So I wrote an input manager class that uses SetWindowsHookEx to connect to WndProc and GetMsg. I believe that the hooks are properly installed, although they look to cause various problems.
Neither my WndProc nor GetMsg hooks receive any messages that a real WndProc receives. My input manager never receives the WM_INPUT, WM_BUTTON, WM_MOUSEWHEEL and WM_KEY * messages that he needs.
What gives?
Partial header:
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); }; }
Partial Source:
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); } }
I do not include code for the message handler, as it consists of 149 lines, most of which are keys for message types. The message values ββreceived in WndProc do not match the values ββin my callbacks.
windows hook messages wndproc setwindowshookex
Sion sheevok
source share