Skip to content

Instantly share code, notes, and snippets.

@arpruss
Last active June 28, 2019 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arpruss/4a315166f8870dfebfa32d0d7ea4b235 to your computer and use it in GitHub Desktop.
Save arpruss/4a315166f8870dfebfa32d0d7ea4b235 to your computer and use it in GitHub Desktop.
Runs specified commands on ctrl-w press
import evdev
from select import select
from sys import argv, exit
from os import system
TRIGGER_KEY = evdev.ecodes.KEY_W
TRIGGER_MODIFIER_DISJUNCTIVE = set((evdev.ecodes.KEY_LEFTCTRL, evdev.ecodes.KEY_RIGHTCTRL))
keysDown = set()
monitored = []
selectList = {}
def getKeyboardDevices():
devices = []
for d in [evdev.InputDevice(path) for path in evdev.list_devices()]:
c = d.capabilities(verbose=True)
try:
if ('KEY_W',17) in c[('EV_KEY', 1)]:
devices.append(d)
except:
pass
return devices
def updateMonitoring():
global monitored,selectList
current = getKeyboardDevices()
changed = False
for d in current:
if d not in monitored:
changed = True
break
if not changed:
for d in monitored:
if d not in current:
changed = True
if changed:
monitored = current
selectList = {d.fd:d for d in current}
return changed
def disjunctiveIn(disjunction, list):
for d in disjunction:
if d in list:
return True
return False
while True:
updateMonitoring()
try:
r, w, x = select(selectList, [], [], 0.25)
for fd in r:
for event in selectList[fd].read():
if event.type == evdev.ecodes.EV_KEY:
if not event.value:
if event.code in keysDown:
keysDown.remove(event.code)
else:
keysDown.add(event.code)
if event.value and event.code == TRIGGER_KEY and disjunctiveIn(TRIGGER_MODIFIER_DISJUNCTIVE, keysDown):
for cmd in argv[1:]:
if cmd == '-exit':
exit(0)
else:
system(cmd)
except KeyboardInterrupt:
exit(0)
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment