Skip to content

Instantly share code, notes, and snippets.

@RenaKunisaki
Created June 3, 2019 01:34
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 RenaKunisaki/0b438105c1a87377863d0dbbcd0628a0 to your computer and use it in GitHub Desktop.
Save RenaKunisaki/0b438105c1a87377863d0dbbcd0628a0 to your computer and use it in GitHub Desktop.
Use trackball as scroll wheel
#!/usr/bin/env python3
# This script opens the given device and maps its
# axis movements to scroll wheel events,
# so you can use a trackball to scroll any window.
# You will need to run `xinput disable $DEVICE` to
# prevent it from also moving the cursor.
DEVICE = "Primax Kensington Eagle Trackball"
import evdev
import pynput
from pynput.mouse import Button, Controller
def main():
global DEVICE
inputDevice = None
# find the device
devices = [evdev.InputDevice(fn) for fn in evdev.list_devices()]
for device in devices:
if device.name == DEVICE:
inputDevice = evdev.InputDevice(device.path)
break
if inputDevice is None:
raise "Device not found"
# get a mouse controller
mouse = Controller()
for event in inputDevice.read_loop():
if event.code == 0: mouse.scroll(event.value / 10, 0)
elif event.code == 1: mouse.scroll(0, event.value / -10)
# 11: wheel
# 272: left click
# 273: right click
#else: print(event.code, event.type, event.value)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment