• SetWindowHookEx的复习


    #include <Windows.h>
    #include <iostream>
    
    using namespace std;
    
    HHOOK keyboardHook;
    
    LRESULT __stdcall KEYHookCallback(int nCode, WPARAM wParam, LPARAM lParam)
    {
        int num[] = { 0x41,0x42,0x43,0x44 ,0x45 ,0x46 ,0x47 ,0x48 ,0x49 ,0x4A ,0x4B ,0x4C ,0x4D ,0x4E ,0x4F,
                        0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,0x58,0x59,0x5A,0x20};
        const char *ch[] = { "A","B", "C", "D", "E", "F", "G", "H","I", "J", "K", "L", "M", "N",
                         "O","P", "Q", "R", "S", "T", "U" ,"V","W", "X", "Y", "Z"," "};
        if (nCode >= 0)
        {
            switch (wParam)
            {
            case WM_KEYDOWN:
            {
                HANDLE stdOut = GetStdHandle(STD_OUTPUT_HANDLE);
                DWORD written = 0;
                KBDLLHOOKSTRUCT* kbdStruct = (KBDLLHOOKSTRUCT*)lParam;
               
                if (stdOut == NULL || stdOut == INVALID_HANDLE_VALUE)
                {
                    cout << "error handle" << endl;
                    return 0;
                }
                POINT pntCurrentCursor;
                GetCursorPos(&pntCurrentCursor);
                HWND h = FindWindow("Notepad", NULL);
                HWND h2 = FindWindowEx(h, 0, "Edit", NULL);
                HWND h1 = WindowFromPoint(pntCurrentCursor);            
                if(h1 == h2)
                {                                                                 
                    for (int i = 0; i < sizeof(num) / sizeof(int); i++)
                    {
                        if (kbdStruct->vkCode == num[i])
                        {
                            WriteConsole(stdOut, ch[i], 1, &written, NULL);
                        }
                    }
                }                    
                return 1;
            }            
            }
        }
        return CallNextHookEx(keyboardHook, nCode, wParam, lParam);
    }
    
    void SetHook()
    {
        if (!(keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KEYHookCallback, NULL, 0)))
        {
            cout << "Failed to install keyboardHook hook!" << endl;
        }
    }
    
    void ReleaseHook()
    {
        UnhookWindowsHookEx(keyboardHook);
    }
    
    int main()
    {
        SetHook();
        MSG msg;
    
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        ReleaseHook();
        return msg.wParam;
    }
  • 相关阅读:
    linux常用命令(持续更新)
    nginx和redis
    网络编程BIO、NIO、AIO
    同步和异步、阻塞和非阻塞
    执行一条sql语句过程
    InnoDB 的B+树索引原理
    InnoDB 为啥要选择B+树来存储数据
    MySQL数据库引擎简介
    java并发编程(同步、同步容器、线程池)
    putty登录linux遭refuse
  • 原文地址:https://www.cnblogs.com/strive-sun/p/12627057.html
Copyright © 2020-2023  润新知