如何在mfc应用程序中使用键盘钩子(Win32中的C ++)

时间:2017-04-28 15:25:52

标签: c++ winapi mfc

如何在mfc应用程序中使用键盘钩子? 我试试这个

typedef struct tagKBDLLHOOKSTRUCT {
    DWORD   vkCode;
    DWORD   scanCode;
    DWORD   flags;
    DWORD   time;
    DWORD   dwExtraInfo;
} KBDLLHOOKSTRUCT, FAR *LPKBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;

LRESULT CALLBACK KeybdProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (wParam == 0x0100)
    {
        KBDLLHOOKSTRUCT *pKey = (KBDLLHOOKSTRUCT*)lParam;
        int key = pKey->vkCode;
        cout << key << " ";
    }
    CallNextHookEx(KbHook, nCode, wParam, lParam);
    return 0;
}

BOOL CUSBDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    cout << "Successfully Loaded !!!" << endl;
    SetIcon(m_hIcon, TRUE);
    SetIcon(m_hIcon, FALSE);

    HMODULE hInstance = GetModuleHandle(NULL);
    KbHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeybdProc, hInstance, 0);

    return TRUE;
}

它引用“成功加载!!!”但是keybdproc无法正常工作。 有人可以帮助我吗?

0 个答案:

没有答案