【问题标题】:Is it possible to use Win32 Hooks in Qt applications是否可以在 Qt 应用程序中使用 Win32 Hooks
【发布时间】:2011-01-11 04:36:15
【问题描述】:

我想知道是否可以在 Qt 应用程序中使用 win32 键盘挂钩函数(SetWindowsHookEx、SetWindowsHookEx)。

如果可能,请提供在 Qt 中使用 SetWindowsHookEx 、 SetWindowsHookEx 函数的示例代码。

//2010 年 2 月 18 日更新 //

我还没有弄清楚如何在 QT 中做到这一点。

但作为一种解决方法,我使用 vc++ express edition 创建了一个 win32 dll,并将我的钩子命令放在 dll 函数中。 我使用 QLibrary 类从 Qt 调用该 dll 函数

 /* 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() 是 ekhook.dll 中被调用的函数

【问题讨论】:

    标签: c++ qt winapi qt4 keyboard-hook


    【解决方案1】:

    我也想知道同样的事情,最后发现了这个。感谢Voidrealms

    该视频的解释足以使用以下代码制作一个工作应用程序。

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

    【讨论】:

      【解决方案2】:

      你不需要对 Qt 做任何事情。只需按照 windows 示例:

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

      【讨论】:

        【解决方案3】:

        我相信这是可能的,是的。使用QWidget::winId

        【讨论】:

        • 如果您能告诉我一个示例代码,展示如何将 Qwidget::winId 与 SetWindowsHookEx 一起使用,那将会很有帮助。我不知道如何将这些结合在一起。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多