Is it possible to use Win32 Hooks in Qt applications
Asked Answered
P

3

9

I like to know if its possible to use win32 keyboard hook function (SetWindowsHookEx , SetWindowsHookEx ) in a Qt application.

If possible pls provide a sample code on using SetWindowsHookEx , SetWindowsHookEx functions in Qt.

//Update as of 18 Feb 2010 //

I havent figured out how to do that in QT yet.

But as a workaround I have created a win32 dll using vc++ express edition and placed my hook commands inside the dll functions. And I call that dll functions from Qt using QLibrary class

 /* 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() is the function in ekhook.dll thats being called

Pack answered 30/1, 2010 at 12:36 Comment(0)
S
5

I was wondering the same thing and found this finally.. Credit goes to Voidrealms.

The video explains enough to make a working application with the following code below.

//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(); 
}
Spray answered 16/8, 2013 at 11:7 Comment(1)
This is not an answer for this question. I'm also searching for multiplatform Qt solution. Mugunth already know WinAPI commands, as he wrote in his question. Question was is there a Qt solution for it?Unwonted
U
3

You don't need to do anything with Qt. Just follow the windows examples:

http://msdn.microsoft.com/en-us/library/ms644960(VS.85).aspx

Unruffled answered 4/2, 2010 at 0:7 Comment(0)
T
1

I believe it is possible, yes. Use QWidget::winId.

Tranship answered 30/1, 2010 at 14:17 Comment(1)
It would be great help if you can let me know a sample code showing how to use Qwidget::winId with SetWindowsHookEx. I am not sure how to us these together.Pack

© 2022 - 2024 — McMap. All rights reserved.