#include <QCoreApplication>
#include <QDebug>
#include <QTime>
#include <QChar>
#include <iostream>
#include <Windows.h>
#pragma comment(lib,"user32.lib")
 
HHOOK hHook = NULL;
 
using namespace std;
 
void UpdateKeyState(BYTE *keystate, int keycode)
{
    keystate[keycode] = GetKeyState(keycode);
}
 
LRESULT CALLBACK MyLowLevelKeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
 
    //WPARAM is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN or WM_SYSKEYUP
    //LPARAM is the key information
   // if( wParam == WM_KEYDOWN )
        //qDebug() << "Key Pressed!";
 
    //Get the key information
 
    KBDLLHOOKSTRUCT cKey = *((KBDLLHOOKSTRUCT*)lParam);
 
    wchar_t buffer[5];
 
    //get the keyboard state
    BYTE keyboard_state[256];
    GetKeyboardState(keyboard_state);
    UpdateKeyState(keyboard_state, VK_SHIFT);
    UpdateKeyState(keyboard_state, VK_CAPITAL);
    UpdateKeyState(keyboard_state, VK_CONTROL);
    UpdateKeyState(keyboard_state, VK_MENU);
 
 
 
    //Get Keyboard Layout
    HKL keyboard_layout = GetKeyboardLayout(0);
 
    //Get the name
    char lpszName[0x100] = {0};
 
    DWORD dwMsg = 1;
    dwMsg += cKey.scanCode << 16;
    dwMsg += cKey.flags << 24;
 
    int i = GetKeyNameText(dwMsg, (LPTSTR)lpszName,255);
    //try to convert the key info
    int result = ToUnicodeEx(cKey.vkCode, cKey.scanCode, keyboard_state, buffer,4,0, keyboard_layout);
    buffer[4] = L'\0';
    //qDebug() << "key:" << cKey.vkCode << " " << QString::fromUtf16((ushort*)buffer) << " " << QString::fromUtf16((ushort*)lpszName);
    int num = cKey.vkCode;
    // Print the output
 
    if( wParam == WM_KEYDOWN )
        //qDebug() << "key:" << cKey.vkCode << " " << QString::fromUtf16((ushort*)buffer) << " " << QString::fromUtf16((ushort*)lpszName);
        return num;
 
 
    return CallNextHookEx(hHook, nCode, wParam, lParam);
}
 
int main(int argc, char *argv[])
{
 
 
    hHook = SetWindowsHookEx(WH_KEYBOARD_LL, MyLowLevelKeyBoardProc, NULL,0);
    if(hHook == NULL){
        qDebug() << "error";
    }
 
    return a.exec();
}
        #include <QCoreApplication>
#include <QDebug>
#include <QTime>
#include <QChar>
#include <iostream>
#include <Windows.h>
#pragma comment(lib,"user32.lib")
HHOOK hHook = NULL;
using namespace std;
void UpdateKeyState(BYTE *keystate, int keycode)
{
    keystate[keycode] = GetKeyState(keycode);
}
LRESULT CALLBACK MyLowLevelKeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    //WPARAM is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN or WM_SYSKEYUP
    //LPARAM is the key information
   // if( wParam == WM_KEYDOWN )
        //qDebug() << "Key Pressed!";
    //Get the key information
    KBDLLHOOKSTRUCT cKey = *((KBDLLHOOKSTRUCT*)lParam);
    wchar_t buffer[5];
    //get the keyboard state
    BYTE keyboard_state[256];
    GetKeyboardState(keyboard_state);
    UpdateKeyState(keyboard_state, VK_SHIFT);
    UpdateKeyState(keyboard_state, VK_CAPITAL);
    UpdateKeyState(keyboard_state, VK_CONTROL);
    UpdateKeyState(keyboard_state, VK_MENU);
    //Get Keyboard Layout
    HKL keyboard_layout = GetKeyboardLayout(0);
    //Get the name
    char lpszName[0x100] = {0};
    DWORD dwMsg = 1;
    dwMsg += cKey.scanCode << 16;
    dwMsg += cKey.flags << 24;
    int i = GetKeyNameText(dwMsg, (LPTSTR)lpszName,255);
    //try to convert the key info
    int result = ToUnicodeEx(cKey.vkCode, cKey.scanCode, keyboard_state, buffer,4,0, keyboard_layout);
    buffer[4] = L'\0';
    //qDebug() << "key:" << cKey.vkCode << " " << QString::fromUtf16((ushort*)buffer) << " " << QString::fromUtf16((ushort*)lpszName);
    int num = cKey.vkCode;
    // Print the output
    if( wParam == WM_KEYDOWN )
        //qDebug() << "key:" << cKey.vkCode << " " << QString::fromUtf16((ushort*)buffer) << " " << QString::fromUtf16((ushort*)lpszName);
        return num;
    return CallNextHookEx(hHook, nCode, wParam, lParam);
}
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    hHook = SetWindowsHookEx(WH_KEYBOARD_LL, MyLowLevelKeyBoardProc, NULL,0);
    if(hHook == NULL){
        qDebug() << "error";
    }
    QString tmp = QString::number(num);
    return a.exec();
}
To copy to clipboard, switch view to plain text mode 
  
Bookmarks