Skip to content

Instantly share code, notes, and snippets.

@if1live
Created May 7, 2014 04:05
Show Gist options
  • Save if1live/9c0cc5343c986686fbd2 to your computer and use it in GitHub Desktop.
Save if1live/9c0cc5343c986686fbd2 to your computer and use it in GitHub Desktop.
Hook mouse event, then generate keyboard event.
#!/usr/bin/env python
#-*- coding: utf-8 -*-
from evdev import UInput, ecodes as e
from asyncore import file_dispatcher, loop
from evdev import InputDevice, categorize, ecodes
from select import select
dev = InputDevice('/dev/input/event4')
print(dev)
devices = [dev]
def press_key(key):
ui = UInput()
# accepts only KEY_* events by default
ui.write(e.EV_KEY, key, 1) # KEY_A down
ui.write(e.EV_KEY, key, 0) # KEY_A up
ui.syn()
ui.close()
while True:
r,w,x = select(devices, [], [])
for fd in r:
for event in dev.read():
if event.code == 272 and event.value == 1:
press_key(e.KEY_A)
print 'press mouse left'
elif event.code == 273 and event.value == 1:
press_key(e.KEY_B)
print 'press mouse right'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment