ubuntu测试键盘工具_在(ubuntu)Linux中捕获第二个键盘输入

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

#include

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;

}

thanks

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

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?

解决方案

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

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.

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"

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值