Created
August 19, 2014 15:37
-
-
Save DerMitch/d1dd19f8945347cb8368 to your computer and use it in GitHub Desktop.
Experiment: Read raw keys from an USB number pad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See this for creating a udev rule to avoid the need to be root: | |
# https://peterkieser.com/2013/07/30/accessing-usb-devices-as-non-root-writing-udev-rules-the-easy-way/ | |
# SUBSYSTEM=="hidraw", ATTRS{idVendor}=="1241", ATTRS{idProduct}=="1503", GROUP="mitch", MODE="0660" | |
keymap = { | |
42: 'BACKSPACE', | |
83: 'NUMLOCK', | |
84: '/', | |
85: '*', | |
86: '-', | |
87: '+', | |
88: 'ENTER', | |
99: 'DEL', | |
89: '1', | |
90: '2', | |
91: '3', | |
92: '4', | |
93: '5', | |
94: '6', | |
95: '7', | |
96: '8', | |
97: '9', | |
98: '0', | |
} | |
def resolve(byte): | |
if byte in keymap: | |
return keymap[byte] | |
return byte | |
def hidraw_reader(filename): | |
with open(filename, "rb") as f: | |
while True: | |
event = f.read(8) | |
# We don't care about the first 2 bytes | |
bytes = [ord(byte) for byte in event][2:] | |
bytes = filter(lambda x: x > 0, bytes) | |
if sum(bytes) == 6: | |
# ! Too many keys pressed | |
pass | |
elif sum(bytes) > 0: | |
yield map(resolve, bytes) | |
if __name__ == '__main__': | |
for keys in hidraw_reader("/dev/hidraw1"): | |
print(keys) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment