Skip to content

Instantly share code, notes, and snippets.

@zacharied
Last active March 21, 2018 06:20
Show Gist options
  • Save zacharied/9428b8894b89e9214e36d87fa58b0427 to your computer and use it in GitHub Desktop.
Save zacharied/9428b8894b89e9214e36d87fa58b0427 to your computer and use it in GitHub Desktop.
Bind foot pedal signals to certain keys. Could be replaced by modifying the udev database to reinterpret the foot pedal as a keyboard.
#!/usr/bin/env python
# Script for binding my foot pedal.
import sys, os, glob
import evdev
from evdev import ecodes
# Search system devices for a pedal.
devices = glob.glob('/dev/input/by-id/usb-VEC_VEC_USB_Footpedal-event-*')
if len(devices) == 0:
print("No pedals found.", file=sys.stderr)
sys.exit(1)
elif len(devices) > 1:
print("More than one pedal found.", file=sys.stderr)
print("Using device '{}'.".format(devices[0]))
pedal = '/dev/input/by-id/' + os.readlink(devices[0])
device = evdev.InputDevice(pedal)
ui = evdev.uinput.UInput()
for event in device.read_loop():
if event.code == 256:
if event.value == 1:
ui.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_BACKSLASH, 1)
ui.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_BACKSLASH, 0)
ui.syn()
if event.code == 257:
if event.value == 1:
ui.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_ESC, 1)
ui.write(evdev.ecodes.EV_KEY, evdev.ecodes.KEY_ESC, 0)
ui.syn()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment