Linux 键盘事件捕获 /dev/inputX

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2775461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:00:31  来源:igfitidea点击:

Linux keyboard event capturing /dev/inputX

linuxlinux-kernellinux-device-driver

提问by Neel Basu

I was trying to capture keyboard events. e.g. I want to drill down a keylogger from the scratch. After 2 hours of fighting I found the following

我试图捕获键盘事件。例如,我想从头开始深入研究键盘记录器。经过 2 个小时的战斗,我发现了以下内容

neel@pc1$ ls -l /dev/input/by-id
lrwxrwxrwx 1 root root 9 2010-05-05 21:33 usb-Plus_More_Enterprise_LTD._USB-compliant_keyboard-event-kbd -> ../event1
lrwxrwxrwx 1 root root 9 2010-05-05 21:33 usb-Plus_More_Enterprise_LTD._USB-compliant_keyboard-event-mouse -> ../event2
lrwxrwxrwx 1 root root 9 2010-05-05 21:33 usb-Plus_More_Enterprise_LTD._USB-compliant_keyboard-mouse -> ../mouse1

But when I tried to

但是当我试图

neel@pc1$ sudo cat /dev/input/usb-Plus_More_Enterprise_LTD._USB-compliant_keyboard-event-kbd

It yields nothing THERE WAS NO OUTPUT

它什么也没产生,没有输出

after a bit more searching Now I am thinking probabbly something in Xorg blocks it.

经过更多的搜索,现在我在想 Xorg 中的某些东西可能会阻止它。

So Any more Information ?? and atthe end of the say how can I read the input from that file ?? or is there any other way of capturing keyboard events ??

那么还有更多信息吗??并在最后说我如何从该文件中读取输入?或者有没有其他方法可以捕获键盘事件?

采纳答案by DzDev

Thank you for the clue about ls -l /dev/input/by-idit helped me a lot !.

感谢您提供有关ls -l /dev/input/by-id它的线索对我帮助很大!

defenderdz@defenderdz-pc:~$ ls -l /dev/input/by-id | grep kbd
lrwxrwxrwx 1 root root  9 nov.  28 14:04 usb-Logitech_USB_Receiver-event-kbd -> ../event7
lrwxrwxrwx 1 root root 10 nov.  29 00:33 usb-NOVATEK_USB_Keyboard-event-kbd -> ../event26
lrwxrwxrwx 1 root root  9 nov.  28 14:04 usb-SONiX_USB_DEVICE-event-kbd -> ../event3
defenderdz@defenderdz-pc:~$ 

'kbd' is the suffix used for keyboard devices (I have 3 keyboards connected).

'kbd' 是用于键盘设备的后缀(我连接了 3 个键盘)。

Your error is that you're accessing the wrong folder:

您的错误是您访问了错误的文件夹

/dev/input/instead of /dev/input/by-id

/dev/input/代替 /dev/input/by-id

In my example the correct path is :

在我的例子中,正确的路径是:

defenderdz@defenderdz-pc:~$ sudo cat /dev/input/by-id/usb-NOVATEK_USB_Keyboard-event-kbd
???]?I???]?I???]?Ia???]?b???]?b???]?b???]?????]?????]??s???]????]????]????]?>
???]?>
 ???]?>
d???]?8
       ???]?8
              ???]?8
                    ???]?????]?????]??s???]H|???]H|???]H|???]?????]?? ???]??d???]????]? ???]?

In your case

在你的情况下

neel@pc1$ sudo cat /dev/input/by-id/usb-Plus_More_Enterprise_LTD._USB-compliant_keyboard-event-kbd

I'm not saying that it's the best solution but it works fine for me. You can even create an automatic detection of the keyboard by parsing the ls result ...

我并不是说这是最好的解决方案,但对我来说效果很好。您甚至可以通过解析 ls 结果来创建键盘的自动检测...

回答by Florian Diesch

You are reading the wrong device. Either try all /dev/input/event* or look in /var/log/Xorg.0.log for which device is used for your keyboard.

您正在读取错误的设备。要么尝试所有 /dev/input/event* 要么在 /var/log/Xorg.0.log 中查找用于您的键盘的设备。

回答by msvilans

Hello,

你好,

I was recently trying to accomplish something similar.

我最近试图完成类似的事情。

Have a look at the logkeys project:

看看 logkeys 项目:

http://code.google.com/p/logkeys/

http://code.google.com/p/logkeys/

If you download the source code, and have a look at the logkeys.cc file, you will find one method how to auto-detect which /dev/input/event is used by your keyboard. This will let you read raw scan codes from the keyboard, regardless of which program currently has focus. The logkeys program also shows how to translate the scan codes into characters, and other useful tricks.

如果您下载源代码,并查看 logkeys.cc 文件,您将找到一种方法来自动检测键盘使用了哪个 /dev/input/event。这将让您从键盘读取原始扫描代码,无论当前哪个程序具有焦点。logkeys 程序还展示了如何将扫描码转换为字符,以及其他有用的技巧。

Hope this helps,

希望这可以帮助,

Markus.

马库斯。

回答by madhat1

I'd recommend using the evtest application, it lists all your input devices and allows you monitor their events.

我建议使用 evtest 应用程序,它会列出您所有的输入设备并允许您监视它们的事件。

回答by Rage5quid

A simple grepoperation on the /proc/bus/input/devicesfile will yield all the keyboards plugged into the machine:

grep/proc/bus/input/devices文件的简单操作将产生所有插入机器的键盘:

 grep -E  'Handlers|EV=' /proc/bus/input/devices | \
 grep -B1 'EV=120013' | \
 grep -Eo 'event[0-9]+'

Where EV=120013is the bitmask for events supported by the device. As explained here.

EV=120013设备支持的事件的位掩码在哪里。正如这里所解释的。

This is the way it is implemented in logkeys

这是它在logkeys 中实现的方式