Skip to content

Instantly share code, notes, and snippets.

@bitfolk
Last active August 29, 2015 14:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bitfolk/c25f1e256f8eae3fa2f9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Quick demo of evdev to snoop on mouse button events.
Full docs at: https://pythonhosted.org/evdev/
To get anything out of this you'll probably need to run it as root.
"""
import sys, getopt
def main(argv):
devfile = ''
try:
opts, args = getopt.getopt(argv, "ld:h")
except getopt.GetoptError:
print 'mousebuttons.py -d <input device>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'mousebuttons.py -d <input device>'
sys.exit()
elif opt in ('-l'):
list_input_devices()
sys.exit()
elif opt in ('-d'):
devfile = arg
if (devfile == ''):
print 'mousebuttons.py -d <input device>'
sys.exit(2)
from evdev import InputDevice, categorize, ecodes
dev = InputDevice(devfile)
print(dev)
for event in dev.read_loop():
if event.type == ecodes.EV_KEY:
print(categorize(event))
def list_input_devices():
from evdev import InputDevice, list_devices
devices = map(InputDevice, list_devices())
for dev in devices:
print( '%-20s %-32s %s' % (dev.fn, dev.name, dev.phys) )
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment