On Wed, 18 Sep 2013 12:00:01 +0100 Mark Rogers mark@quarella.co.uk wrote:
I have a cheap barcode scanner that works well as a keyboard wedge - that is it appears as a keyboard and barcodes appear as though typed into the keyboard.
I need to capture that data (and stop it appearing as keyboard data). In an ideal world it would appear as a TTY (serial) device instead.
Is there any way I can do this or do I need a different scanner?
You can stop the default Xorg HID input driver (evdev) from using it as a keyboard:
http://askubuntu.com/questions/17603/how-to-get-xorg-to-ignore-certain-input...
Then you need to read the key press events from the /dev/input/eventX device node and possibly translate into a virtual terminal device (pty) for the application to read. There are useful symbolic links in the /dev/input hierarchy that make it easier to find your device, and reading events is this trivial (I was debugging a weird keyboard when I wrote this code):
// From kernel docs: // https://www.kernel.org/doc/Documentation/input/input.txt #include <stdio.h> #include <fcntl.h> #include <linux/input.h>
int main(int argc, char **argv) { struct input_event evt; int fd; char *dev = "/dev/input/event5"; if (argc>1) dev = argv[1]; if((fd = open(dev, O_RDONLY))<0) { perror("opening device"); return 1; } while(read(fd, &evt, sizeof(evt))==sizeof(evt)) { printf("type=%d code=%d value=%d (0x%08x)\n", evt.type, evt.code, evt.value, evt.value); } close(fd); return 0; }
Sending to the chosen application may be trickier: depends if it can be persuaded to read from a network connection or named pipe (easy), or if it needs a real terminal node, in which case openpty() and friends will be needed to fool the app into thinking it's talking to a serial port.
Phil.