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.