Re: [ALUG] Intercepting data from HID (keyboard wedge barcode scanner)
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.
On 18 September 2013 14:33, Phil Ashby <phil.ashby@bt.com> wrote:
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.
Thanks for this, I can now see raw data using: cat /dev/input/event13 | hexdump -C .. which is a start, although I need to parse the events. Are there any libraries (I assume there are somewhere) for parsing keyboard event data?
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.
At present the application doesn't exist so that gives me some flexibility :-) Mark -- Mark Rogers // More Solutions Ltd (Peterborough Office) // 0844 251 1450 Registered in England (0456 0902) @ 13 Clarke Rd, Milton Keynes, MK1 1LG
participants (2)
-
Mark Rogers -
Phil Ashby