创建全局键盘钩

时间:2015-04-19 18:46:55

标签: c++ winapi dll

我正在尝试创建一个全局键盘钩子,用于读取我正在创建的热键程序的击键,我遇到了一些问题。首先是每当我激活我的钩子时,键盘变得迟钝。屏幕上出现的每个角色之间几乎有一整秒的延迟。第二个问题是我在DLL回调函数中编写的任何代码都不会被执行。

这是我的代码:

DLL cpp文件

#include<Windows.h>
#include "keyDLL.h"

using namespace std;

LRESULT CALLBACK HookProcedure(int code, WPARAM wParam, LPARAM lParam)
{
    return CallNextHookEx(0, code, wParam, lParam);
}

DLL头文件

#include "windows.h"

extern "C" __declspec(dllexport) LRESULT CALLBACK HookProcedure(int code, WPARAM wParam, LPARAM lParam);

挂钩CPP文件

#include<Windows.h>
#include<iostream>
using namespace std;

HHOOK WINAPI SetWindowsHookEx(_In_  int idHook, _In_  HOOKPROC lpfn, _In_  HINSTANCE hMod, _In_  DWORD dwThreadId
static HINSTANCE dllHandle;
HOOKPROC address;
static HHOOK keyboard_hook;

int main()
{
    dllHandle = LoadLibrary(TEXT("keyDLL.dll"));
    address = (HOOKPROC)GetProcAddress(dllHandle, "HookProcedure");
    cout << address << endl << dllHandle << endl;
    keyboard_hook = SetWindowsHookEx(WH_KEYBOARD_LL, address, dllHandle, 0);

    system("pause");
}

这个版本的代码只是一个超级简单的剪切版本,只设置了键盘钩子。我试图找出导致问题的原因,我拿出了所有其他代码。根据我对钩子的理解(这显然不是那么明显),这段代码应该挂钩键盘但不做任何击键操作。我不明白为什么它会让键盘放慢速度。我使用dumpbin来确保DLL导出函数没有名称错误。我正在运行Windows 8 64位,我的DLL和我的exe文件都编译为64位。当我输出地址和dllHandle变量时,我获得了内存地址,所以我知道至少部分代码正在运行。任何帮助将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:5)

另一种方法是下面的代码:

#define _WIN32_WINNT 0x0400
#pragma comment( lib, "user32.lib" )

#include <iostream>
#include <windows.h>
#include <stdio.h>

HHOOK hKeyboardHook;

__declspec(dllexport) LRESULT CALLBACK KeyboardEvent (int nCode, WPARAM wParam, LPARAM lParam)
{
    DWORD SHIFT_key=0;
    DWORD CTRL_key=0;
    DWORD ALT_key=0;


    if  ((nCode == HC_ACTION) &&   ((wParam == WM_SYSKEYDOWN) ||  (wParam == WM_KEYDOWN)))      
    {
        KBDLLHOOKSTRUCT hooked_key =    *((KBDLLHOOKSTRUCT*)lParam);
        DWORD dwMsg = 1;
        dwMsg += hooked_key.scanCode << 16;
        dwMsg += hooked_key.flags << 24;
        char lpszKeyName[1024] = {0};

        int i = GetKeyNameText(dwMsg,   (lpszKeyName+1),0xFF) + 1;

        int key = hooked_key.vkCode;

        SHIFT_key = GetAsyncKeyState(VK_SHIFT);
        CTRL_key = GetAsyncKeyState(VK_CONTROL);
        ALT_key = GetAsyncKeyState(VK_MENU);

            printf("Keycode = %c\n",key);

        if (key >= 'A' && key <= 'Z')   
        {

            if  (GetAsyncKeyState(VK_SHIFT)>= 0) key +=32;

            /*********************************************
            ***   Hotkey scope                         ***
            ***   do stuff here                        ***
            **********************************************/

            if (CTRL_key !=0 && key == 'y' )
            {
               MessageBox(NULL, "CTRL-y was pressed\nLaunch your app here", "H O T K E Y", MB_OK); 
               CTRL_key=0;

               //do stuff here




            }

            if (CTRL_key !=0 && key == 'q' )
            {
                MessageBox(NULL, "Shutting down", "H O T K E Y", MB_OK); 
               PostQuitMessage(0);
            }




            SHIFT_key = 0;
            CTRL_key = 0;
            ALT_key = 0;

        }

    }
    return CallNextHookEx(hKeyboardHook,    nCode,wParam,lParam);
}

void MessageLoop()
{
    MSG message;
    while (GetMessage(&message,NULL,0,0)) 
    {
        TranslateMessage( &message );
        DispatchMessage( &message );
    }
}

DWORD WINAPI my_HotKey(LPVOID lpParm)
{
    HINSTANCE hInstance = GetModuleHandle(NULL);
    if (!hInstance) hInstance = LoadLibrary((LPCSTR) lpParm); 
    if (!hInstance) return 1;

    hKeyboardHook = SetWindowsHookEx (  WH_KEYBOARD_LL, (HOOKPROC) KeyboardEvent,   hInstance,  NULL    );
    MessageLoop();
    UnhookWindowsHookEx(hKeyboardHook);
    return 0;
}

int main(int argc, char** argv)
{
    HANDLE hThread;
    DWORD dwThread;

    printf("CTRL-y  for  H O T K E Y  \n");
    printf("CTRL-q  to quit  \n");

    hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)   my_HotKey, (LPVOID) argv[0], NULL, &dwThread);

       /* uncomment to hide console window */
    //ShowWindow(FindWindowA("ConsoleWindowClass", NULL), false);

    if (hThread) return WaitForSingleObject(hThread,INFINITE);
    else return 1;

}