按回车键导出键盘挂钩消息

Press enter to export Keyboard Hook message

我写了一个程序来获取键盘钩子。我想要的是,当用户按下 Enter 时,输入的文本,例如:我存储在 exportMsg 中的“hello world”,应该从函数返回。

我想制作一个dll并导出exportMsg

这是我的代码。提前致谢。

#include <Windows.h>
#include <stdio.h>
#include <iostream> 
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam); 
HHOOK keyboardHook;
HWND prevWindow;
std::string exportMsg=""; 
int main()
{
    keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, 0, 0);
    MSG msg{ 0 };
    while (GetMessage(&msg, NULL, 0, 0) != 0);
    UnhookWindowsHookEx(keyboardHook);
    return 0;
} 
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    wchar_t title[256];
    HWND fwindow = GetForegroundWindow();
    PKBDLLHOOKSTRUCT key = (PKBDLLHOOKSTRUCT)lParam;
    //a key was pressed
    if (wParam == WM_KEYDOWN && nCode == HC_ACTION )
    { 
        //return if enter pressed
        if (key->vkCode == '\r')
        {  
            std::cout << exportMsg << std::endl;
        }
        else
        {
            exportMsg.push_back(key->vkCode);
        }
    }
    return CallNextHookEx(keyboardHook, nCode, wParam, lParam);
}

由于 std::cinUnity 中不起作用,我需要使用键盘钩子通过 QR Code Scanner Machine 扫描二维码并将扫描的字符串发送到Unity.

此算法捕获击键和 returns 按下 Enter 时的字符串。

我的 DLL 代码如下所示:

头文件:

LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
HHOOK keyboardHook;
HWND prevWindow;
std::string exportMsg = "";

QRCODEREADER_EXPORTS_API void StartHook(char* str, int strlen);

源文件:

QRCODEREADER_EXPORTS_API void StartHook(char* str, int strlen)
{ 
    string result;
    cout << "please scan the QR code!!!\n";
    //std::cin >> result; 
    exportMsg.clear();

    HHOOK keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, GetModuleHandle(NULL), 0);
    if (!keyboardHook) {
        std::cout << "Failed to hook keyboard\n";
    }
    else {
        MSG msg{ 0 };
        while (GetMessage(&msg, NULL, 0, 0) != 0);
         
        result = exportMsg;
        cout << "your data: " << result << "\n";

        result = result.substr(0, strlen);
        //str = new char[result.length()];
        std::copy(result.begin(), result.end(), str);
        str[min(strlen - 1, (int)result.size())] = 0;
    } 
     
    std::cout << "Quitting...\n"; 
    UnhookWindowsHookEx(keyboardHook); 
}
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    HWND fwindow = GetForegroundWindow();
    PKBDLLHOOKSTRUCT key = (PKBDLLHOOKSTRUCT)lParam;
    //a key was pressed
    if (wParam == WM_KEYDOWN && nCode == HC_ACTION)
    {
        //return if enter pressed
        if (key->vkCode == '\r')
        {
            std::cout << exportMsg << std::endl;
            PostQuitMessage(0); 
            UnhookWindowsHookEx(keyboardHook);
        }
        else
        {
            exportMsg.push_back(key->vkCode);
        }
    }
    return CallNextHookEx(keyboardHook, nCode, wParam, lParam);
}

dllmain:

int main() {
    char* a;

    for (int i = 0; i < 3; i++)
    {
        a = new char[50];
        StartHook(a, 50);
        cout<< i << "th iter: " << a << endl;
    } 
    return 0;
}