c++winapidllkeyboard-hook

Press enter to export Keyboard Hook message


I've written a program to get keyboard hook. What I want is, when a user presses Enter, the typed text, for example: "hello world", which I stored in exportMsg, should be returned from the function.

I want to make a dll and export exportMsg.

Here is my code. Thanks in advance.

#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);
}

Solution

  • As std::cin doesn't work in Unity, I needed to use Keyboard Hook to scan qrCode through a QR Code Scanner Machine and send the scanned string to the Unity.

    This algorithm capture the key strokes and returns the string when Enter is pressed.

    My DLL code looks like this:

    Header file:

    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);
    

    Source file:

    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;
    }