Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.