2010-01-30 18 views
7

Me gustaría saber si es posible utilizar la función de gancho de teclado win32 (SetWindowsHookEx, SetWindowsHookEx) en una aplicación Qt.¿Es posible utilizar Win32 Hooks en aplicaciones Qt?

Si es posible, los pls proporcionan un código de muestra sobre el uso de las funciones SetWindowsHookEx, SetWindowsHookEx en Qt.

// datos a fecha de 18 de Feb 2010 //

no he descubierto la manera de hacer eso en QT todavía.

Pero como solución alternativa he creado un dll win32 usando vC++ express edition y coloqué mis comandos hook dentro de las funciones dll. y pido que las funciones DLL desde Qt usando la clase 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() es la función de eso que se llama ekhook.dll

Respuesta

1

creo que es posible, sí. Use QWidget::winId.

+1

Sería de gran ayuda si me puede dejar saber un código de muestra que muestra cómo usar Qwidget :: winId con SetWindowsHookEx. No estoy seguro de cómo hacer esto juntos. – Mugunth

4

Me preguntaba lo mismo y encontré este fin .. El crédito va a Voidrealms.

El video explica lo suficiente como para hacer una aplicación que funcione con el siguiente código a continuación.

//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(); 
} 
Cuestiones relacionadas