zoukankan      html  css  js  c++  java
  • Windows键盘钩子

    Runtime:VS2013

    #include "stdafx.h"
    #include <windows.h>
    #include <conio.h>
    
    DWORD g_main_tid = 0;
    HHOOK g_kb_hook = 0;
    
    bool CALLBACK con_handler(DWORD CtrlType)
    {
        PostThreadMessage(g_main_tid, WM_QUIT, 0, 0);
        return TRUE;
    };
    
    LRESULT CALLBACK kb_proc(int code, WPARAM w, LPARAM l)
    {
        PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)l;
        const char *info = NULL;
        if (w == WM_KEYDOWN)
            info = "key dn";
        else if (w == WM_KEYUP)
            info = "key up";
        else if (w == WM_SYSKEYDOWN)
            info = "sys key dn";
        else if (w == WM_SYSKEYUP)
            info = "sys key up";
        printf("%s - vkCode [%04x], scanCode [%04x]
    ",
            info, p->vkCode, p->scanCode);
        // always call next hook
        return CallNextHookEx(g_kb_hook, code, w, l);
    };
    
    DWORD WINAPI KbHookThread(LPVOID lpParam)
    {
        g_main_tid = GetCurrentThreadId();
    
        //register Ctrl+C callback
        SetConsoleCtrlHandler((PHANDLER_ROUTINE)&con_handler, TRUE);
        g_kb_hook = SetWindowsHookEx(WH_KEYBOARD_LL, //Installs a hook procedure that monitors low-level keyboard input events
            (HOOKPROC)&kb_proc, //A hook procedure that monitors low-level keyboard input events
            NULL,//A handle to the DLL containing the hook procedure pointed to by the lpfn parameter. The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process.(GetModuleHandle(NULL))
            0);//The identifier of the thread with which the hook procedure is to be associated. For desktop apps, if this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread. For Windows Store apps, see the Remarks section.
        if (g_kb_hook == NULL) {
            printf("SetWindowsHookEx failed, %ld
    ", GetLastError());
            return 0;
        }
    
        // Message loop
        MSG msg;
        
    
        //If GetMessage retrieves the WM_QUIT message, the return value is zero.
        while (GetMessage(&msg, NULL, 0, 0)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        
        //exit the thread
        UnhookWindowsHookEx(g_kb_hook);
        return 0;
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        HANDLE h = CreateThread(NULL, 0, KbHookThread, NULL, 0, NULL);
        if (NULL == h) {
            printf("CreateThread failed, %ld
    ", GetLastError());
            return -1;
        }
    
        //wait thread to exit
        WaitForMultipleObjects(1, &h, TRUE, INFINITE);
    
        return 0;
    }
  • 相关阅读:
    开发笔记:python与随机数(转)
    如何建立内核级钩子控制操作系统实现程序隐身(转)
    SPOJ 7001. Visible Lattice Points (莫比乌斯反演)
    BZOJ 2301: [HAOI2011]Problem b (莫比乌斯反演)
    HDU 1695 GCD (莫比乌斯反演)
    HDU 4691 Front compression (2013多校9 1006题 后缀数组)
    HDU 4686 Arc of Dream (2013多校9 1001 题,矩阵)
    HDU 1695 GCD (欧拉函数+容斥原理)
    【转】[专题学习][计算几何]
    POJ 1755 Triathlon (半平面交)
  • 原文地址:https://www.cnblogs.com/LubinLew/p/Windows-Hook-Keyboard.html
Copyright © 2011-2022 走看看