如何在Linux中获取键盘状态? [英] How can I get the keyboard state in Linux?

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

问题描述

我想检查用户在程序启动时是否按下 Shift 键. (这意味着在程序启动前按下 Shift 键.)这是一个简单的控制台程序,与X无关.

I want to check if the user pressed down the Shift key when the 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 the 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 键.具有其他键盘布局的人们可能必须使用 Shift 修饰符才能访问数字.如果您的程序对此 Shift 键作出反应,则您的程序基本上无法使用.同样的情况也适用于其他修饰键:只有在按下普通字符键之后,您才能检测到其中的某些修饰键.或更糟糕的是,他们可能需要使用 Shift 键来使用输入"来运行程序.

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.

如果您是root用户并想监视本地计算机上的 Shift 键,则可以读取evdev设备以获取有关 Shift 键的事件.但这仅是由于自动重复按键而可能实现的,因此您不会检测到在运行程序之前立即按下的 Shift 键,而仅在此之前几秒钟.

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天全站免登陆