linux下按键码一致吗

发布于 2024-12-19 03:57:40 字数 1580 浏览 3 评论 0原文

我目前正在尝试创建一个小的 C/C++ 程序,它模拟多媒体键之一上的按键(例如“暂停/播放”)。

为了模拟按键,我使用了 X11 库中的 XTestFakeKeyEvent 函数。我在这里找到了一个工作示例: Simulate keypress in a Linux C console application

我的问题是,我打算模拟的那些特殊键在 keysymdef.h 文件中找不到,其中使用的常量XKeysymToKeycode-函数已定义。

所以,我做了一些研究,发现 这篇文章,它把我带到了xmodmap-命令。使用 xmodmap -pk 得到了一个包含这些键的列表:

KeyCode Keysym (Keysym) ...
Value   Value   (Name)  ...
[...]
171     0x1008ff17 (XF86AudioNext)  0x0000 (NoSymbol)   0x1008ff17 (XF86AudioNext)  
172     0x1008ff14 (XF86AudioPlay)  0x1008ff31 (XF86AudioPause) 0x1008ff14 (XF86AudioPlay)  0x1008ff31 (XF86AudioPause) 
173     0x1008ff16 (XF86AudioPrev)  0x0000 (NoSymbol)   0x1008ff16 (XF86AudioPrev)  
174     0x1008ff15 (XF86AudioStop)  0x1008ff2c (XF86Eject)  0x1008ff15 (XF86AudioStop)  0x1008ff2c (XF86Eject)  
[...]

使用这些定义的值(例如用于播放/暂停的 172)作为 XTestFakeKeyEvent 的键码/code>-function 我让它工作:

// Simulate Key-Press:
Display *display;
display = XOpenDisplay(NULL);

XTestFakeKeyEvent(display, 172, true, 0);
XTestFakeKeyEvent(display, 172, false, 0);
XFlush(display);

现在,我的问题是:

我可以依赖这些值(整数)映射到每个 Linux 系统上的这些键吗?如果不是(这是我的猜测),动态获取正确映射(在代码中)的正确方法是什么?

I'm currently trying to create a little C/C++ program which emulates a key-press on one of the Multimedia-Keys (e.g. "Pause/Play").

To simulate the keypress I used the XTestFakeKeyEvent-function from the X11-library's. I found a working example here on SO: Simulate keypress in a Linux C console application

My problem is, that those special keys which I intend to simulate are not to be found in the keysymdef.h-file, where the constants for the used XKeysymToKeycode-function are defined.

So, I did a little research and found this post, which brought me to the xmodmap-command. Using xmodmap -pk I got a list which does include those keys:

KeyCode Keysym (Keysym) ...
Value   Value   (Name)  ...
[...]
171     0x1008ff17 (XF86AudioNext)  0x0000 (NoSymbol)   0x1008ff17 (XF86AudioNext)  
172     0x1008ff14 (XF86AudioPlay)  0x1008ff31 (XF86AudioPause) 0x1008ff14 (XF86AudioPlay)  0x1008ff31 (XF86AudioPause) 
173     0x1008ff16 (XF86AudioPrev)  0x0000 (NoSymbol)   0x1008ff16 (XF86AudioPrev)  
174     0x1008ff15 (XF86AudioStop)  0x1008ff2c (XF86Eject)  0x1008ff15 (XF86AudioStop)  0x1008ff2c (XF86Eject)  
[...]

Using those defined values (like 172 for play/pause) as the keycodes for the XTestFakeKeyEvent-function I got it to work:

// Simulate Key-Press:
Display *display;
display = XOpenDisplay(NULL);

XTestFakeKeyEvent(display, 172, true, 0);
XTestFakeKeyEvent(display, 172, false, 0);
XFlush(display);

Now, my question is:

Can I rely on those values (the integers) to be mapped to these keys on every linux system? If not (which is what I guess), what would be the proper way to get the correct mappings dynamically (in code)?

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

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

发布评论

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

评论(1

心头的小情儿 2024-12-26 03:57:41

我知道这不是一个明确的答案,但根据我的经验(类似项目),我尝试过的每台 Linux 机器上的键映射都是相同的。这仅适用于具有 EN/US 键盘布局的机器。我不能谈论具有替代布局的键盘。

如果这不能完全回答您的问题,我很抱歉。

编辑:

我实际上查看了我的旧项目,看起来我使用这些函数来获取实际的键码:

XStringToKeysym()
XKeysymToKeycode()

看看手册页,它们非常不言自明。

我希望这有帮助:)

I know this is not a definitive answer, but in my experience (with a similar project) the key mappings have been the same on every Linux machine I've tried it on. This has been only on machines with EN/US keyboard layout. I can't speak about keyboards with alternative layouts.

I'm sorry if this does not answer your question completely.

EDIT:

I actually looked at my old project and it looks like I used these functions to get the actual keycodes:

XStringToKeysym()
XKeysymToKeycode()

Look at the MAN pages, they are pretty self explanatory.

I hope this helped :)

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