如何以编程方式检测linux中大写锁定的状态 [英] How to detect the status of the capslock in linux programmatically
问题描述
我想知道 Capslock 是否处于活动状态,我想我可以使用 xet
为此目的,使用管道,通过 popen('xset -q | grep Capslock')
我能找出来,但我想要一些不使用命令的方法,在 C 程序中,有什么方法可以知道这一点.在这种情况下我还要问一件事,xset
在 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 是否处于活动状态
I want some way to know if the Capslock is active or not
您可能想要 XkbGetIndicatorState
.例如:
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;
}
或者,您可以使用 相同的方法在 xset 中使用 并使用 XkbGetNamedIndicator
这是一个更通用的函数.
Alternatively, you can go with the same approach that is used in xset and use XkbGetNamedIndicator
which is a more general function.
这篇关于如何以编程方式检测linux中大写锁定的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!