在 Linux 上使用 X11 抓取特定按键上的事件

发布于 2024-10-09 08:22:52 字数 388 浏览 6 评论 0原文

我正在用 C++ 编写一个程序,以在运行 Linux 发行版的 MacBook Pro 上实现 OS X 的键盘背光功能。到目前为止,它会在启动时打开背光,如果 20 秒内没有注册键盘和鼠标事件,它将重新关闭背光,当然,当注册事件时,它会再次打开。我需要程序做的下一件事是捕获键盘背光向上/向下键上的按键,但我不确定如何解决这个问题。

我目前正在使用 XScreenSaverQueryInfo 来获取键盘和鼠标事件的空闲时间,因此使用 X11 API 的方法就可以了。我已经做了很多谷歌搜索,但还没有找到一种我确信可以采用的方法。我发现的许多方法的问题是它们使用键码来识别键,但我认为这不是一个可行的解决方案,因为该程序应该适用于任何可用的键盘布局。

我知道我应该使用什么方法和 API 吗?什么最有效?

问候,

I'm writing a program in C++ to implement the keyboard backlight feature from OS X on MacBook Pro's running a Linux distro. So far, it turns the backlight on, on boot and if no keyboard and mouse events are registered for 20 seconds, it will turn it back off, and of course turn it on yet again when an event is registered. Next thing I need the program to do, is to capture keypresses on the keyboard-backlight-up/down keys, but I'm not sure how to approach this.

I am currently using XScreenSaverQueryInfo to get the idle time of keyboard and mouse events, so a method using X11 API would be okay. I have done a lot of googling but havent found a way that I felt sure about going with. The problem I'm seeing with lots of the methods I found, is that they use keycode to identify the key, but I dont think that would be a viable solution since the program should work for any keyboard-layout available.

Any idea of a method and API I should go with? What would work the best?

Regards,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清浅ˋ旧时光 2024-10-16 08:22:52

执行此操作的正常方法是使用 XGrabKey()。它使用键码,但您不会对键码进行硬编码,您可以使用 XKeysymToKeycode() 获得它。为了更正确,您还希望在收到 MappingNotify (XMappingEvent) 时重做抓取。 (注意,MappingNotify,不是 MapNotify。)如果这些键没有键符号 - 旧的 X 版本上可能没有,但希望较新的 X.org 版本有一个 - 那么你只需必须硬连线键码。它不是很强大或便携,但可能适用于具有相同硬件型号的 Linux 上的每个人。

请做好准备,密钥抓取是全局的,因此如果您尝试 XGrabKey() 并且其他东西已经抓取了该密钥,您将收到 X 错误 - 默认情况下会退出程序。 XGrabKey() 的另一个怪癖是它使用精确的修饰符集来获取密钥。例如,要处理有或没有 NumLock 的情况,您需要抓取两次。请参阅 使用 X11/Xlib 的全局热键

在正常的 Linux 设置中(如果您想要为了将这样的功能添加到上游项目中),桌面环境不希望大量单独的应用程序争夺密钥并出现错误。因此,将会有一些中央协调点,例如窗口管理器或特殊的守护进程可能会执行所有键绑定并根据需要将命令转发到其他进程。因此,如果您试图将功能默认集成到发行版中,您可能需要考虑修补处理其他特殊键的相同上游代码。

另一件需要注意的事情是 Xkb API,它要复杂得多。有一些令人费解的方法可以用 Xkb 获取密钥,但我不知道走这条路有什么好处。

The normal way to do this is with XGrabKey(). It uses keycodes, but you wouldn't hardcode the keycode, you'd get it with XKeysymToKeycode(). To be more correct you'd also want to redo the grab when you get a MappingNotify (XMappingEvent). (Note, MappingNotify, not MapNotify.) If there isn't a keysym for these keys - there probably isn't on old X versions, but hopefully newer X.org versions have one - then you just have to hardwire the keycode. Which won't be very robust or portable but probably works for everyone on Linux with the same hardware model.

Be prepared that key grabs are global, so if you try to XGrabKey() and something else has already grabbed that key, you'll get an X error - by default that exits the program. Another quirk of XGrabKey() is that it grabs the key with a precise modifier set. For example, to handle both with and without NumLock, you need to grab twice. See Global Hotkey with X11/Xlib

In a normal Linux setup (if you wanted to get a feature like this into upstream projects), the desktop environments don't want lots of separate apps fighting over the key grabs and getting errors. So there will be some central coordination points, such as the window manager or a special daemon might do all the keybindings and forward commands to other processes as needed. So you would probably want to look at patching the same upstream code that handles other special keys like this, if you were trying to get your feature integrated into distributions by default.

Another thing to be aware of is the Xkb API, which is a lot more complicated. There is some brain-bending way to grab keys with Xkb but I don't know of any advantage to going that route.

不离久伴 2024-10-16 08:22:52

如果您还没有这样做,请熟悉 xev。启动它,给它焦点,然后按下按键,看看发生了什么。

If you haven't done that yet, familiarize yourself with xev. Start it, give it the focus, and press the keys, to see what's happening.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文