本文介绍了SetWindowsHookEx,KeyboardProc和非静态成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个键盘钩,其中KeyboardProc是类CWidget的静态成员。
I am creating a keyboard hook, wherein KeyboardProc is a static member of a class CWidget.
class CWidget
{
static LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam );
};
我想在CWidget :: KeyboardProc中调用CWidget的非静态成员。
I want to call the non-static members of CWidget inside the CWidget::KeyboardProc.
最好的方法是什么?
KeyboardProc没有任何32位DWORD,我可以在其中传递 this指针。
KeyboardProc does not have any 32 bit DWORD where I can pass the 'this' pointer.
推荐答案
鉴于您可能一次只想安装一个键盘钩,只需在您的班级中添加一个静态pThis成员即可:
Given that you probably only want one keyboard hook installed at a time, just add a static pThis member to your class:
// Widget.h
class 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. */
};
您需要定义静态成员:
// Widget.cpp
CWidget *CWidget::m_pThis = NULL;
这篇关于SetWindowsHookEx,KeyboardProc和非静态成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!