SetWindowsHookEx, KeyboardProc and Non-static members
Given that you probably only want one keyboard hook installed at a time, just add a static pThis member to your class:
// Widget.hclass CWidget{ static HHOOK m_hHook; static CWidget *m_pThis;public: /* NOT static */ bool SetKeyboardHook() { m_pThis = this; m_hHook = ::SetWindowsHookEx(WH_KEYBOARD, StaticKeyboardProc, /* etc */); } // Trampoline static LRESULT CALLBACK StaticKeyboardProc(int code, WPARAM wParam, LPARAM lParam) { ASSERT(m_pThis != NULL); m_pThis->KeyboardProc(code, wParam, lParam); } LRESULT KeyboardProc(int code, WPARAM wParam, LPARAM lParam); /* etc. */};
You need to define the static member:
// Widget.cppCWidget *CWidget::m_pThis = NULL;