挂钩键&使用 Qt 4.6 的键盘组合键 [英] Hook key & key combinations from keyboard with Qt 4.6

查看:12
本文介绍了挂钩键&使用 Qt 4.6 的键盘组合键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个无窗口应用程序,它在任务栏上只有一个图标(Windows、Mac OS X 和 Linux).我希望它捕获一些关键 &组合键,比如说 Right Control + Right Shift.键入正确的组合后,它会做一些事情,比如截图.我可以做无窗口应用程序、任务栏上的图标和屏幕截图,但我不知道如何全局监控键盘的组合键.请多多指教.非常感谢任何帮助或提示!提前致谢!

Let's say I have a window-less application which has only an icon on the Taskbar (Windows, Mac OS X & Linux). I want it to capture some key & key combinations, let's say Right Control + Right Shift. Upon keying in correct, combination, it will do something, say take screenshot. I can do window-less app, icon on the Taskbar and screen capture but I don't know how to monitor keyboard globally for key combinations. Please kindly advise. Any help or hint is greatly appreciated! Thanks in advance!

推荐答案

系统范围的密钥抓取是一个棘手的问题,但系统范围的密钥挂钩更棘手.每个 OS/GUI 都有自己的解决方案,至少对于抓取来说是这样.Qt4 没有公开这样的特性,但是 Qt eXTension 库 用它的 QxtGlobalShortcut.这是一个很好的包装:

System-wide key grabbing is a tricky subject, but system-wide key hooking is even trickier. Every OS/GUI has its own solution, at least for grabbing. Qt4 doesn't expose such feature, but Qt eXTension library solves the problem with its QxtGlobalShortcut. It's a nice wrapper for:

  • XGrabKey()/XUngrabKey() in X11,
  • RegisterHotKey()/UnregisterHotKey() in Windows,
  • RegisterEventHotKey()/UnregisterEventHotKey() in Mac OS X.

因此您可以获取其他应用程序无法获得的显式组合键,即特定的键和修饰符(XGrabKey() 允许更多).此处不支持按键序列,即连续的按键组合.

So you can grab explicit key combination, i.e. particular key and modifiers (XGrabKey() allows a bit more), that no other application will get. Key sequences, i.e. consecutive key combinations, are not supported here.

键盘挂钩功能更强大,因为它允许查看输入事件(甚至过滤它们).它不仅被键盘记录器使用,而且是这里的典型关联.

Keyboard hooking is much more powerful, because it allows peeking at the input events (or even filtering them). It's not only used by keyboard loggers, but they are a typical association here.

如果您使用 Windows,那么您可以阅读:

If you're into Windows, then you can read:

  • Hooks and DLLs by Joseph M. Newcomer,
  • Hooks.

在 X11 中要复杂得多.您可能至少要阅读两页:

In X11 it's much more complicated. There are at least a two pages you may want to read:

  • X.Org Wiki - Development/Documentation/InputEventProcessing - to have some background,
  • Exploiting X11 to monitor keystrokes - to understand difficulties.

有一个 X 事件拦截扩展,但没有得到维护,最终得到了删除.

There was a X Event Interception Extension, but it wasn't maintained and eventually has been removed.

希望它可以在没有 X11 基础架构的帮助下完成.在 Linux 2.6 内核中有事件接口",称为 evdev,可以在这里利用.详细信息可以在 logkeys Linux 键盘记录器的源代码中找到.它也可以通过类似于 evdev 的效果来完成.查看我的 PoC 项目:kaos - 关键活动屏幕显示.

Hopefully it can be done without the help of X11 infrastructure. In Linux 2.6 kernel there is "Event interface", known as evdev, that can be exploited here. Details can be found in the source code of the logkeys Linux keylogger. It can also be done with something in effect similar to evdev. See my PoC project: kaos - Key Activity On-Screen display.

而且我没有 Mac,所以没有进一步的参考.;)

And I don't have Mac, so no further references. ;)

这篇关于挂钩键&使用 Qt 4.6 的键盘组合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆