如何使Linux在使程序可供阅读的同时忽略键盘? [英] How to make Linux ignore a keyboard while keeping it available for my program to read?

查看:61
本文介绍了如何使Linux在使程序可供阅读的同时忽略键盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建某种信息亭系统,为此我购买了该USB DIY键盘: https://www.amazon.com/gp/product/B07QPXQQ7L

I am building some kind of kiosk system and I bought this USB DIY keyboard for it: https://www.amazon.com/gp/product/B07QPXQQ7L

这使我有很多按钮,它们的行为类似于键盘按键.

This allows me to have a lot of buttons and they behave like keyboard keys.

我正在编写一个程序(Perl),该程序将从键盘上获取输入,并以此为基础进行操作.

I'm writing a program (Perl) that will take the input from that keyboard and do things based on that.

问题是我需要让系统的其余部分(X和TTY)都忽略该键盘,以便它不会在终端或窗口管理器中键入随机内容.换句话说,系统应该忽略它,但设备本身仍必须在/dev/input/...

The problem is that I need to have the rest of the system (both X and the TTYs) ignore that keyboard so that it won't type random things in the terminal or in the window manager. In other words, the system should disregard it but the device itself must still be available in /dev/input/...

我不需要真正的键盘来控制机器,因为我通过VNC和SSH连接.

I don't need a real keyboard to control the machine because I connect via VNC and SSH.

如果您知道如何从/dev/input/...键盘进行读取并以与终端中的STDIN相似的方式输入字母,则奖励积分.

Bonus points if you know how to read from a /dev/input/... keyboard and end up with letters typed just like with STDIN in a terminal.

谢谢!

推荐答案

我在这里找到了解决方案,在使用条形码读取器的情况下,有人想要完全一样的东西: https://serverfault.com/questions/385260/bind-usb键盘专用于特定应用程序/976557#976557

I found the solution here where someone wanted the exact same thing in the case of a barcode reader: https://serverfault.com/questions/385260/bind-usb-keyboard-exclusively-to-specific-application/976557#976557

SUBSYSTEM=="input", ACTION=="add", ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="yyyy", RUN+="/bin/sh -c 'echo remove > /sys$env{DEVPATH}/uevent'"
ACTION=="add", ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="yyyy", SYMLINK+="diykeyboard"

然后用 lsusb 中的供应商和产品ID替换 xxxx yyyy .因此,以我为例 1c4f 0002 :

And then replace xxxx and yyyy by the Vendor and Product ID as found in lsusb. So in my case 1c4f and 0002:

Bus 001 Device 003: ID 1c4f:0002 SiGma Micro Keyboard TRACER Gamma Ivory

udevadm控件--reload 的事情对我而言不起作用,我必须重新启动.

The udevadm control --reload thing didn't do it for me, I had to reboot.

然后从理论上讲,在/dev/diykeyboard (SYMLINK变量)上应该可以找到在键盘上键入的数据.

Then in theory the data typed on the keyboard should be available at /dev/diykeyboard (the SYMLINK variable).

不幸的是,在我的情况下,现在有多个事件与此供应商+产品匹配,并且与我需要添加 DEVPATH =="*:1.0/*",KERNEL =="event *" 在第二行中创建SYMLINK.然后惊奇的是它没有在/dev中创建链接,所以我不得不做点脏事,自己用ln创建一个链接:

Now in my case unfortunately there are multiple events that match this vendor+product, and to match the right one I needed to add DEVPATH=="*:1.0/*", KERNEL=="event*" in the second line where it creates the SYMLINK. And then surprise it did not create the link in /dev so I had to do something dirty, create a link myself with ln:

SUBSYSTEM=="input", ACTION=="add", ATTRS{idVendor}=="1c4f", ATTRS{idProduct}=="0002", RUN+="/bin/sh -c 'echo remove > /sys$env{DEVPATH}/uevent'"
SUBSYSTEM=="input", ACTION=="add", ATTRS{idVendor}=="1c4f", ATTRS{idProduct}=="0002", DEVPATH=="*:1.0/*", KERNEL=="event*", RUN+="/bin/sh -c 'ln -sf /dev/input/$kernel /diykeyboard'"

(不要在/tmp中创建链接,因为udev发生在引导时/tmp的安装之前)

(don't create the link in /tmp since udev happens before the mounting of /tmp at boot)

从那里我可以使用 evtest /diykeyboard (通常指向/dev/input/event0 )中读取内容,该内容显示了键入的密钥,或直接在我的程序中键入的密钥,然后对扫描代码进行解码.

From there I can read from /diykeyboard (which usually points to /dev/input/event0) either with evtest which shows the keys typed, or directly with my program and then decoding the scancodes.

这篇关于如何使Linux在使程序可供阅读的同时忽略键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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