如何获得在Linux下键盘状态? [英] How to get keyboard state in Linux?

查看:202
本文介绍了如何获得在Linux下键盘状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查,如果用户pressed下来的 SHIFT 的关键,程序启动时。 (也就是说,preSS下的 SHIFT 的程序之前键启动),这是一个简单的控制台程序,没有相关于X。

I want to check if the user pressed down the SHIFT key, when program starts. (That means, press down the SHIFT key before the program is started) It's a simple console program, nothing related to X.

这可能类似于Win32的 GetKeyboardState()功能。

This maybe similar to Win32 GetKeyboardState() function.

我想知道我是否能做到这一点,怎么样,但是没有任何优劣直接访问该终端。

I want to know whether I can do this and how, but not any pros and cons with accessing the terminal directly.

推荐答案

您不能。

Shift键不被视为一个字符键,所以即使你直接访问终端,你将无法检测到该密钥。

The shift key isn't considered as a character key, so even if you access the terminal directly, you won't be able to detect this key.

也许你不应该有。想象一下,例如,您使用的是美式键盘,其中数字是没有修饰顶行访问,也检查shift键。与其它键盘布局的人可能需要使用移修饰符来访问的数字。如果你的程序应对这种转变preSS,那么你的程序基本上是无法使用。同样的道理也适用于其他组合键:你可能只后的正常字符键检测其中的一些是pressed。更糟的是,他们可能需要使用Shift键使用ENTER键运行程序。

Maybe you shouldn't have to. Imagine for example that you are using a US keyboard where numbers are accessible on the top row without modifiers, and also checking for the shift key. People with other keyboard layout may have to use shift modifiers to access the numbers. If your program react to this shift press, then your program is basically unusable. The same thing applies for other modifier keys : you may detect some of them only after a normal character key is pressed. Or worse, they may need to use the shift key to use 'enter' to run your program.

另外,你想要什么shift键来监控?一个在本地机器上,或者一个用户所在?请记住,SSH存在,并且通常用于远程访问伪终端。

Also, what shift key do you want to monitor ? the one on the local machine, or the one where the user is ? remember that SSH exists and is commonly used to access a pseudoterminal remotely.

如果你是根,要监视的本地机器上的Shift键,可以读出了evdev的设备有关shift键事件。但这仅仅是可能的,因为自动密钥的重复,这样你就不会检测到shift键这是正确的运行程序之前pssed $ P $,但只有几秒钟之前。

If you are root and want to monitor the Shift key on the local machine, you can read the evdev devices for events about the shift key. But this is only possible because of automatic key repeating, so you won't detect a shift key that is pressed right before running your program, but only a few second before.

当然,你不能这样做,在远程机器上,这将是一个安全漏洞。

Of course you can't do that on the remote machine, that would be a security flaw.

不管怎样,你为什么会想这样做?不会的X应用程序在你的情况做正确的事?

And anyway, why would you want to do that ? wouldn't an X application be the right thing to do in your case ?

这篇关于如何获得在Linux下键盘状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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