A USB scanner, which presents as a keyboard is at:
lsusb | grep -i scanner
Bus 250 Device 005: ID 0c2e:9a6c 0c2e Metrologic Scanner
Is there an example of python script that can record the 'keystrokes' from the scanner only into a variable for further parsing? Example or links to examples are appreciated: thank you
In Unix, evdev would handle these events, however, this mechanism is absent in OS X.
This excerpt was from another posting:
Code from http://docs.python.org/faq/library.html#how-do-i-get-a-single-keypress-at-a-time
#!/usr/bin/python
import termios, fcntl, sys, os
fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
try:
while 1:
try:
c = sys.stdin.read(1)
print "Got character", repr(c)
except IOError: pass
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
One more solution https://stackoverflow.com/questions/11918999/key-listeners-in-python