Trapping second keyboard input in (ubuntu) linux

2019-04-04 17:04发布

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;
}

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?

4条回答
We Are One
2楼-- · 2019-04-04 17:30

It's been a while since this question has been asked :) Anyway, I think what you should do is to use the linux input device subsystem API.

http://www.linuxjournal.com/article/6429 here's a good introduction.

查看更多
姐就是有狂的资本
3楼-- · 2019-04-04 17:33

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.

查看更多
我只想做你的唯一
4楼-- · 2019-04-04 17:37

If I have understood your question correctly, there may be a few problems corresponding to what you want to do.

1) In order to read from these files in /dev folder you need to have root permissions.

2) (I am not too sure about this) but I believe these are special files and hence you cannot read them as you would a normal file.

Assuming you took care of these two problems , it will still not solve your problem because X events are handled by the X sever, which you can think of as simultaneously reading the same file. It is the one which captures these events and handles them accordingly by calling the relevant event handlers, if any, for a particular event in the topmost active window. All the windows talk to the X server which tells if something has been typed. So even if you have a terminal window open with a program running, first the X server must tell the window about the key presses which will then be passed to the program running in the terminal.

Another code which does similar work can be found here.

查看更多
▲ chillily
5楼-- · 2019-04-04 17:38

I'll get started with a list of common problems surrounding your task, I don't have the answer, but I can at least provide some light on why you are having problems.

  1. Keyboard devices, for obvious security reasons, have access control restrictions on them. For obvious reasons, if arbitrary applications could sniff/hook the keyboard without the right permission, it could have fatal consequences, AKA: Keyboard Logger.

  2. Sometimes, when one application ( in your case X ) has gained control of an input device, it eats up all the bytes being sent to it. So if you managed to get around the permissions problem, you still have a problem in that some other software is consuming the datastream before you.

查看更多
登录 后发表回答