7

I have barcode scanner, attached to Linux computer via USB. The scanner emulates a keyboard device.

I have to write a program that to read the scanned barcodes and process them. The program runs on background as a service and should read the barcode scanner regardless of the current X focus.

How this can be made in Linux?

Some lower level solution/explanation is preferred.

johnfound
  • 6,857
  • 4
  • 31
  • 60

2 Answers2

2

It sounds like you want to capture the data from a specified device, In which case the method described in this post should help:

(EDIT: original link dead, Archive link provided)

https://web.archive.org/web/20190101053530/http://www.thelinuxdaily.com/2010/05/grab-raw-keyboard-input-from-event-device-node-devinputevent/

That will listen out for keyboard events stemming from only the specified source.

A word of caution though, as far as I know, that won't stop it from propagating to whatever your current window focus is.

Minothor
  • 316
  • 4
  • 15
  • well, passing the barcodes to the focused application is highly undesirable. – johnfound Feb 06 '17 at 13:10
  • Short of cancelling the keypress event after reading so it doesn't bubble up, I'm not sure how you'd do it, I had a look for ways of restricting device nodes to one application only but it's not proving too fruitful so far. – Minothor Feb 06 '17 at 13:46
  • closest thing I've found so far is this person gaining exclusive control over a device, intercepting and modifying the key events: https://www.reddit.com/r/linux/comments/1h30c8/devinputeventx_c_programming_question/ Actually, that might be what you're looking for: `... If you maintain the grab, nothing else will see the keystrokes. ...` – Minothor Feb 06 '17 at 13:47
  • The link is dead. Anyone could provide the solution mentioned in there I personally hope for a bash/shell solution – dj_thossi Mar 18 '20 at 19:24
  • @dj_thossi cheers for letting me know, updated it with the last intact version from archive.org. – Minothor Mar 18 '20 at 20:12
  • Thank you. Would anyone know how to solve the same challenge in Bash or PHP? – dj_thossi Mar 19 '20 at 07:15
  • Afraid not, I looked into it for PHP a few years ago when I was going to make a more flexible inventory system for a PoS thing in the states, but that project ended before the research was done. I was just going to accept the input as a general keyboard input (which many scanners emulate) rather than trying to specifically filter out the attached keyboard from the the scanner. – Minothor Mar 20 '20 at 14:16
0

To start with solution, I guess a daemon would perfect choice.

You can write a daemon code, which will open device node (for scanner) and read the data buffer.

Now you have received data in user space, you are free to handle it as per your requirement.

BhanuSingh
  • 118
  • 1
  • 2
  • 15