我想知道是否可以在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中被调用的函数
发布于 2013-08-16 19:07:50
我也在想同样的事情,最后找到了这个..功劳归功于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();
}发布于 2010-02-04 08:07:15
你不需要用Qt做任何事情。只需按照windows示例进行操作:
http://msdn.microsoft.com/en-us/library/ms644960(VS.85).aspx
发布于 2010-01-30 22:17:36
我相信这是可能的,是的。使用QWidget::winId。
https://stackoverflow.com/questions/2167876
复制相似问题