I've built a custom keyboard with a trackball under my thumb and I want to map a scroll behavior in such way that when I press a shortcut (like left alt) and rotate the ball, it should behave like a scroll. I'm using Xorg here, but I'd be very interested in seing solutions for both X and Wayland (if the solution would involve display server).
Emulate vertical and horizontal scrolling using
evdev driver from dirkt's answer only supports vertical scrolling emulation. If you (like me) also want to scroll horizontally, the
libinput driver can be used to do that:
The details are in the manpage for
Option "ScrollButton" "int"
Designates a button as scroll button. If the ScrollMethod is button and the button is logically held down, x/y axis movement is converted into scroll events.
Option "ScrollMethod" "string"
Enables a scroll method. Permitted values are none, twofinger, edge, button. Not all devices support all options, if an option is unsupported, the default scroll option for this device is used
Section "InputClass" Identifier "Some Trackball" # ... Stuff to match your trackball, see xorg.conf(5) Driver "libinput" Option "ScrollMethod" "button" Option "ScrollButton" "4" # Or whatever button suits your needs EndSection
Simulate mouse wheel with trackball
You can either use existing configuration options of drivers that do something similar to what you want, or write your own driver.
The Logitech M570 is a wireless trackball with an USB-receiver. That means it will show up as a HID device, and mouse events will be made available via the input layer in the kernel, and can read from the corresponding
The X driver in turn uses the
evdev driver to read events from this layer, and turns them into X events. I'm not familiar with Wayland, but I assume it will work similarly there.
evdev driver already contains options to emulate a mousewheel on a trackball, see
man evdev. The most important ones are
Option "EmulateWheel" "boolean"
Enable/disable "wheel" emulation. Wheel emulation means emulating button press/release events when the mouse is moved while a specific real button is pressed. Wheel button events (typically buttons 4 and 5) are usually used for scrolling. Wheel emulation is useful for getting wheel-like behaviour with trackballs.
Option "EmulateWheelButton" "integer"
Specifies which button must be held down to enable wheel emulation mode. While this button is down, X and/or Y pointer movement will generate button press/release events as specified for the XAxisMapping and YAxisMapping settings. If the button is 0 and EmulateWheel is on, any motion of the device is converted into wheel events. Default: 4.
So this works using a trackball button instead of keyboard shortcut (because the driver only sees events from the mouse, not from the keyboard), and will convert movement into wheel events, and not rotation.
2) Write your own driver by modifying several existing ones. You will need to combine the drivers for the keyboard and the mouse, so wheel emulation can be triggered by keypresses.
The source code for the kernel input layer is in
drivers/input in the kernel tree.
The source code for the evdev X driver is in xorg/driver/xf86-input-evdev/tree/src/evdev.c.
The keyboard driver depends on what type of keyboard you used.