2010-01-30 6 views
7

Mi piacerebbe sapere se è possibile utilizzare la funzione di hook della tastiera win32 (SetWindowsHookEx, SetWindowsHookEx) in un'applicazione Qt.È possibile utilizzare Win32 Hooks nelle applicazioni Qt

Se possibile, fornire un codice di esempio sull'utilizzo delle funzioni SetWindowsHookEx, SetWindowsHookEx in Qt.

// Update come del 18 Feb 2010 //

Io ho mai capito come farlo in QT ancora.

Ma come soluzione alternativa ho creato una win32 dll utilizzando vC++ express edition e ho inserito i miei comandi hook all'interno delle funzioni dll. e chiamo che funzioni DLL da Qt utilizzando la classe QLibrary

/* hearder file code*/ 
    QLibrary *myLib; 
    typedef HHOOK (*MyPrototype)(HINSTANCE); 

/* source file code */ 
    myLib = new QLibrary("ekhook.dll"); 
    MyPrototype myFunction; 
    myFunction = (MyPrototype) myLib->resolve("Init"); 

init() è la funzione di questo è ekhook.dll chiamato

risposta

1

credo che sia possibile, sì. Utilizzare QWidget::winId.

+1

Sarebbe di grande aiuto se puoi farmi sapere un codice di esempio che mostra come usare Qwidget :: winId con SetWindowsHookEx. Non sono sicuro di come farci questi insieme. – Mugunth

4

mi chiedevo la stessa cosa ed ho trovato questo, infine, .. merito va a Voidrealms.

Il video spiega quanto basta per creare un'applicazione funzionante con il seguente codice qui sotto.

//Copied Code from YouTube Video 

#include <QtCore/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 

    qDebug() << "Key Pressed!"; 

    if (wParam == WM_KEYDOWN) 
    { 
     //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 information 
     int result = ToUnicodeEx(cKey.vkCode, cKey.scanCode, keyboard_state, buffer, 4, 0, keyboard_layout); 
     buffer[4] = L'\0'; 

     //Print the output 
     qDebug() << "Key: " << cKey.vkCode << " " << QString::fromUtf16((ushort*)buffer) << " " << QString::fromUtf16((ushort*)lpszName); 

    } 

    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() << "Hook Failed" << endl; 
    } 

    return a.exec(); 
}