USB – HID human interface device
With this python script you can capture keyboard (emulators) and barcode scanners trough evdev (event device), this way the part of code will only execute when the device is sending keys:
1 2 3 4 5 6 7 8 |
from evdev import * dev = InputDevice('/dev/input/event1') print(dev) for event in dev.read_loop(): if event.type == ecodes.EV_KEY: print(categorize(event)) |
output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
key event at 1383327570.147000, 2 (KEY_1), down key event at 1383327570.147990, 2 (KEY_1), up key event at 1383327570.148997, 3 (KEY_2), down key event at 1383327570.150010, 3 (KEY_2), up key event at 1383327570.151009, 29 (KEY_LEFTCTRL), down key event at 1383327570.151009, 42 (KEY_LEFTSHIFT), down key event at 1383327570.152017, 36 (KEY_J), down key event at 1383327570.153005, 36 (KEY_J), up key event at 1383327570.154004, 29 (KEY_LEFTCTRL), up key event at 1383327570.155005, 32 (KEY_D), down key event at 1383327570.155993, 32 (KEY_D), up key event at 1383327570.157002, 48 (KEY_B), down key event at 1383327570.158015, 48 (KEY_B), up key event at 1383327570.158997, 48 (KEY_B), down key event at 1383327570.282002, 18 (KEY_E), up key event at 1383327570.283004, 49 (KEY_N), down key event at 1383327570.284005, 49 (KEY_N), up key event at 1383327570.284968, 18 (KEY_E), down |
Another python script example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import evdev from evdev import InputDevice, categorize # import * is evil :) dev = InputDevice('/dev/input/event1') # Provided as an example taken from my own keyboard attached to a Centos 6 box: scancodes = { # Scancode: ASCIICode 0: None, 1: u'ESC', 2: u'1', 3: u'2', 4: u'3', 5: u'4', 6: u'5', 7: u'6', 8: u'7', 9: u'8', 10: u'9', 11: u'0', 12: u'-', 13: u'=', 14: u'BKSP', 15: u'TAB', 16: u'Q', 17: u'W', 18: u'E', 19: u'R', 20: u'T', 21: u'Y', 22: u'U', 23: u'I', 24: u'O', 25: u'P', 26: u'[', 27: u']', 28: u'CRLF', 29: u'LCTRL', 30: u'A', 31: u'S', 32: u'D', 33: u'F', 34: u'G', 35: u'H', 36: u'J', 37: u'K', 38: u'L', 39: u';', 40: u'"', 41: u'`', 42: u'LSHFT', 43: u'\\', 44: u'Z', 45: u'X', 46: u'C', 47: u'V', 48: u'B', 49: u'N', 50: u'M', 51: u',', 52: u'.', 53: u'/', 54: u'RSHFT', 56: u'LALT', 100: u'RALT' } for event in dev.read_loop(): if event.type == evdev.ecodes.EV_KEY: data = evdev.categorize(event) # Save the event temporarily to introspect it if data.keystate == 1: # Down events only key_lookup = scancodes.get(data.scancode) or u'UNKNOWN:{}'.format(data.scancode) # Lookup or return UNKNOWN:XX print u'You Pressed the {} key!'.format(key_lookup) # Print it all out! |
Output: …