如何以编程方式检测linux中的capslock状态(How to detect the status of the capslock in linux programmatically)

编程入门 行业动态 更新时间:2024-10-18 20:20:16
如何以编程方式检测linux中的capslock状态(How to detect the status of the capslock in linux programmatically)

我想通过某种方式知道Capslock是否处于活动状态,认为我可以使用xet ,使用管道,通过popen('xset -q | grep Capslock')我能够找到,但我想要一些方法在没有使用命令的情况下,在C程序中,有没有办法知道这一点。 还有一件事我想在这个上下文中问一下, xset dons不能在linux的控制台模式下工作,我做alt + ctrl + f1然后登录那里如果尝试运行xset -q这会抛出错误,也许这可以'在控制台中与XWindows进行通信,因此对于这种情况可以采用什么解决方案。

I want some way to know if the Capslock is active or not, thought I can use xet for this purpose, using pipe, by popen('xset -q | grep Capslock') I am able to find out, but I want some way by which there is no use of the commands, in the C program, is there any way to know this. One more thing I want to ask in this context, xset doens't work in the console mode in linux, I do alt+ctrl+f1 then login there and if try to run xset -q this will throw error, perhaps this can't communicate with the XWindows in console, so what solution can be for this case.

最满意答案

我想知道Capslock是否有效

你可能想要XkbGetIndicatorState 。 例如:

#include <stdio.h> #include <stdlib.h> #include <X11/XKBlib.h> /* Compile this with -lX11 */ int main () { Display *display; Status status; unsigned state; display = XOpenDisplay (getenv ("DISPLAY")); if (!display) return 1; if (XkbGetIndicatorState (display, XkbUseCoreKbd, &state) != Success) return 2; printf ("Caps Lock is %s\n", (state & 1) ? "on" : "off"); return 0; }

或者,您可以使用xset中使用的相同方法并使用XkbGetNamedIndicator ,这是一个更通用的功能。

I want some way to know if the Capslock is active or not

You probably want XkbGetIndicatorState. For instance:

#include <stdio.h> #include <stdlib.h> #include <X11/XKBlib.h> /* Compile this with -lX11 */ int main () { Display *display; Status status; unsigned state; display = XOpenDisplay (getenv ("DISPLAY")); if (!display) return 1; if (XkbGetIndicatorState (display, XkbUseCoreKbd, &state) != Success) return 2; printf ("Caps Lock is %s\n", (state & 1) ? "on" : "off"); return 0; }

Alternatively, you can go with the same approach that is used in xset and use XkbGetNamedIndicator which is a more general function.

更多推荐

本文发布于:2023-08-04 10:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415203.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:状态   方式   linux   programmatically   capslock

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!