读取 Linux 中键盘修饰符的物理状态(最好使用 C 或 C++)

读取 Linux 中键盘修饰符的物理状态(最好使用 C 或 C++)

有没有一种方法可以以编程方式读取、检测或获取有关实际身体状态修饰键的,无需 root 级别访问权限权利?

我尝试了 X11、XCB 和 XKB,但它们的事件驱动方法仅在修饰键已物理释放或以其他方式使用(例如在鼠标单击期间)后注册修饰键更改。

我正在寻找一种能够异步读出例如SHIFT按键按下/释放状态的方法,而不必等待“消耗”的下一个事件发生。如果 X11/XCB/XKB 不允许这样做,我也会考虑进入 HID 方向,但在这样做之前,我想澄清一下,如果我没有忽略一些简单的东西,比如非阻塞修饰符事件模式或类似的东西只需简单地打开即可。

编辑 (2023-09-30):

我现在还查看了XInputv2 及其原始键输入支持,但没有多大帮助。事件处理显然已经从驱动程序级别继承了这种行为。

下一步是调查 的输出evtest,这似乎证实了这一理论:如果驱动程序层不同步处理/通信相关状态更改,则甚至不可能异步查看物理状态。

这是终端输出的副本,您可以在其中看到修改器按下/释放仅相隔几毫秒,而实际上我们按下它们的时间比这更长:

Testing ... (interrupt to exit)
Event: time 1696090563.122466, type 4 (EV_MSC), code 4 (MSC_SCAN), value 1c
Event: time 1696090563.122466, type 1 (EV_KEY), code 28 (KEY_ENTER), value 0
Event: time 1696090563.122466, -------------- SYN_REPORT ------------
Event: time 1696090567.475603, type 4 (EV_MSC), code 4 (MSC_SCAN), value 2a
Event: time 1696090567.475603, type 1 (EV_KEY), code 42 (KEY_LEFTSHIFT), value 1
Event: time 1696090567.475603, -------------- SYN_REPORT ------------
Event: time 1696090567.487761, type 4 (EV_MSC), code 4 (MSC_SCAN), value 2a
Event: time 1696090567.487761, type 1 (EV_KEY), code 42 (KEY_LEFTSHIFT), value 0
Event: time 1696090567.487761, -------------- SYN_REPORT ------------
Event: time 1696090570.051134, type 4 (EV_MSC), code 4 (MSC_SCAN), value 2a
Event: time 1696090570.051134, type 1 (EV_KEY), code 42 (KEY_LEFTSHIFT), value 1
Event: time 1696090570.051134, -------------- SYN_REPORT ------------
Event: time 1696090570.062911, type 4 (EV_MSC), code 4 (MSC_SCAN), value 2a
Event: time 1696090570.062911, type 1 (EV_KEY), code 42 (KEY_LEFTSHIFT), value 0
Event: time 1696090570.062911, -------------- SYN_REPORT ------------
Event: time 1696090574.108130, type 4 (EV_MSC), code 4 (MSC_SCAN), value 2a
Event: time 1696090574.108130, type 1 (EV_KEY), code 42 (KEY_LEFTSHIFT), value 1
Event: time 1696090574.108130, -------------- SYN_REPORT ------------
Event: time 1696090574.119016, type 4 (EV_MSC), code 4 (MSC_SCAN), value 2a
Event: time 1696090574.119016, type 1 (EV_KEY), code 42 (KEY_LEFTSHIFT), value 0
Event: time 1696090574.119016, -------------- SYN_REPORT ------------
Event: time 1696090576.276169, type 4 (EV_MSC), code 4 (MSC_SCAN), value 1d
Event: time 1696090576.276169, type 1 (EV_KEY), code 29 (KEY_LEFTCTRL), value 1
Event: time 1696090576.276169, -------------- SYN_REPORT ------------
Event: time 1696090576.288810, type 4 (EV_MSC), code 4 (MSC_SCAN), value 1d
Event: time 1696090576.288810, type 1 (EV_KEY), code 29 (KEY_LEFTCTRL), value 0
Event: time 1696090576.288810, -------------- SYN_REPORT ------------
Event: time 1696090577.396269, type 4 (EV_MSC), code 4 (MSC_SCAN), value 38
Event: time 1696090577.396269, type 1 (EV_KEY), code 56 (KEY_LEFTALT), value 1
Event: time 1696090577.396269, -------------- SYN_REPORT ------------
Event: time 1696090577.407817, type 4 (EV_MSC), code 4 (MSC_SCAN), value 38
Event: time 1696090577.407817, type 1 (EV_KEY), code 56 (KEY_LEFTALT), value 0
Event: time 1696090577.407817, -------------- SYN_REPORT ------------

相关内容