如何使用Qt禁用Windows(全局)中的密钥?

是否可以禁用或禁止诸如打印屏幕键之类的键?

我的事件filter:

bool EventFilter::eventFilter(QObject *object, QEvent *event) { qDebug() << "object:" << object << "type:" << event->type(); return false; } 

我尝试在qApp使用:

 ui->setupUi(this); EventFilter *evt = new EventFilter; qApp->installEventFilter(evt); 

但是只返回来自应用程序小部件的事件:

 object: QWidgetWindow(0x175bae50, name = "QWidgetClassWindow") type: QEvent::Type(PlatformSurface) object: QWidget(0x175b02c0) type: QEvent::Type(PlatformSurface) object: QWidget(0x175b02c0) type: QEvent::Type(WinIdChange) object: QWidget(0x175b02c0) type: QEvent::Type(Create) ... 

和:

 ui->setupUi(this); EventFilter *evt = new EventFilter; QDesktopWidget *c = new QDesktopWidget; c->installEventFilter(evt); 

但是只返回2个事件:

 object: QDesktopWidget(0x174e0260, name = "desktop") type: QEvent::Type(PolishRequest) object: QDesktopWidget(0x174e0260, name = "desktop") type: QEvent::Type(Polish) 

不能拦截和/或阻止事件? 谢谢

您可以安装一个低级别的键盘挂钩来拦截和屏蔽所有的Print Screen印刷机。 以下是我的Windows 7机器上测试。

这是一个简单的例子,展示了如何做到这一点:

截图

 #include <QtWidgets> #include <windows.h> //link against user32.lib when compiling in MSVC #ifdef _MSC_VER #pragma comment(lib, "User32.lib") #endif class GlobalPrintScreenBlocker { public: GlobalPrintScreenBlocker():mHKeyboardHook(NULL) {} //to avoid leaking the hook procedure handle ~GlobalPrintScreenBlocker(){ unblock(); } //hook callback function (called on every system-wide key press) static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { if(nCode == HC_ACTION) { PKBDLLHOOKSTRUCT p = reinterpret_cast<PKBDLLHOOKSTRUCT>(lParam); if(p->vkCode == VK_SNAPSHOT) return 1; //block print-screen key } //this is not a message we are interested in return CallNextHookEx(NULL, //ignored paramater nCode, wParam, lParam); } void block(){ mHKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, //low-level keyboard hool &LowLevelKeyboardProc, //callback GetmoduleeHandle(NULL), 0); } void unblock(){ if(mHKeyboardHook) UnhookWindowsHookEx(mHKeyboardHook); mHKeyboardHook = NULL; } private: HHOOK mHKeyboardHook; }; int main(int argc, char* argv[]) { QApplication a(argc, argv); GlobalPrintScreenBlocker blocker; QPushButton button("Disable ScreenShot"); button.setCheckable(true); QObject::connect(&button, &QPushButton::toggled, [&](bool isChecked){ if(isChecked) blocker.block(); else blocker.unblock(); }); button.show(); return a.exec(); } 

钩子在安装它的线程中被调用。 这样做的优点是不必担心回调函数中的线程安全问题,并且在32位进程和64位进程之间没有区别(所有进程最终都会通过发布消息到安装钩子的线程的事件循环)。 但是,这也有一个缺点,如果你的线程忙(执行其他任何事情)回调挂钩可能不会被调用:

挂钩过程应该在比LowLevelHooksTimeout值中指定的数据条目更少的时间内处理消息…该值以毫秒为单位。 如果挂钩程序超时,系统将消息传递给下一个挂钩。 但是,在Windows 7和更高版本中,挂钩将被静默移除而不被调用。 应用程序无法知道挂钩是否被移除。

你可以通过分配一个单独的线程来安装/执行钩子来解决这个限制。 此外,请注意您的钩子的回调函数应尽可能最小,以便能够在时间限制内完成。

PS:我不得不使用剪切工具来创建上面的屏幕截图。 。 。