在(ubuntu)Linux中捕获第二个键盘输入 [英] Trapping second keyboard input in (ubuntu) linux

查看:410
本文介绍了在(ubuntu)Linux中捕获第二个键盘输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个程序,该程序从usb第二个键盘(实际上是条形码扫描仪)获取输入.问题是,如果另一个窗口处于活动状态,则数据将输入到那里而不是在我的程序中.有人可以给我关于我做错了什么的建议吗?

I have written a program that gets input from a usb second keyboard (actually a barcode scanner). The problem is that if another window is active the data is input there rather than in my program. Could someone give me advice on what I'm doing wrong?

#include <stdio.h>
#include <string.h>

int main(int argc, char * argv[]){
   FILE * fp_in;
   char * data;
   fp_in = fopen("/dev/input/by-id/usb-04d9_1400-event-kbd","r");

   if(fp_in == NULL){
      fprintf(stderr,"Failed to open input by id\n");
   }

   fp_in = fopen("/dev/input/by-path/pci-0000:00:1d.1-usb-0:2:1.0-event-kbd","r");

   if(fp_in == NULL){
      fprintf(stderr,"Failed to open input by path\n");
      return 1;
   }

  while(1){
      fscanf(fp_in,data,"%s");
      fprintf(stderr,"%s",data);
  }
  return 0;
}

谢谢


如果我敢于以Confuzzled的名义改写这个问题:

thanks


If I may be so bold as to rephrase the question on Confuzzled's behalf:

我如何在Linux下编写一个程序,该程序将自身连接到输入设备(在本例中为条形码扫描仪),以使输入不会进入具有键盘焦点的程序?

How can I write a program under Linux that attaches itself to an input device, in this case a barcode scanner, so that the input does not go to the program that has the keyboard focus?

推荐答案

我试图做同样的事情,我所做的是使用xinput浮动"该设备.就我而言,xinput list显示(除其他外)

I was trying to do the same thing, What I did was to "float" that device using xinput. In my case, xinput list shows (among other things)

HID Keyboard Device HID Keyboard Device id=13 [slave keyboard (3)]

这是与条形码扫描仪相对应的设备.然后,您只需键入

This is the device the corresponds to the barcode scanner. You can then simply type

xinput float 13

进入终端.扫描仪的击键将不再输入到聚焦窗口中,但仍可以从设备文件中读取它们.但是,您需要对从文件中读取的事件进行解码,以获取所需的信息(条形码).有关如何操作的某些信息,请参见格式/dev/input/event *?这个.

into a terminal. Keystrokes from the scanner will no longer get entered into the focused window, but they can still be read from the device file. However, you will need to decode the events you read from the file to get the information you want(the barcode). See format of /dev/input/event*? for some information on how to do this.

最后,要在没有root特权的情况下读取设备文件,只需为扫描程序添加udev规则.对我来说,是这样的:

Finally, to read the device file without root privileges, just add a udev rule for the scanner. For me, it's something like this:

SUBSYSTEM=="input", ATTRS{idVendor}=="1d57", ATTRS{idProduct}=="001c" MODE="0644"

插入扫描仪后,通过检查dmesg的输出,可以找到扫描仪的idVendor和idProduct.

The idVendor and idProduct for your scanner can be found by examining the output of dmesg after plugging the scanner in.

这篇关于在(ubuntu)Linux中捕获第二个键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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