Skip to content

Instantly share code, notes, and snippets.

@mike10004
Created May 7, 2015 18:55
Show Gist options
  • Save mike10004/3aa7640e8a0f6031ca8c to your computer and use it in GitHub Desktop.
Save mike10004/3aa7640e8a0f6031ca8c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# inspired by http://stackoverflow.com/questions/5060710/format-of-dev-input-event
import struct
from argparse import ArgumentParser
if __name__ == '__main__':
p = ArgumentParser()
p.description = 'Read events from /dev/input and print them to console. Must be run as root.'
p.add_argument("--source", default="/dev/input/event0", help="specify source (entry in /dev/input/*; default is event0)")
args = p.parse_args()
with open(args.source, 'r') as source:
index = 0
event_format = "llHHI"
event_len = struct.calcsize(event_format)
event = source.read(event_len)
while event:
(tv_sec, tv_usec, evt_type, code, value) = struct.unpack(event_format, event)
print "%4d %8u %8u %8u" % (index, evt_type, code, value)
index += 1
event = source.read(event_len)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment