这是我之前在个人网站上发布的一篇旧博客文章。我正在将所有内容转移到优快云。感谢阅读!
This project is one step forward toward setting up universal mouse steering in racing games on Linux. Some games come with mouse support out of the box, while other games require additional setup. The game in question is Rigs of Rods – an open-source vehicle sandbox-simulator that is available on both Windows and Linux platforms. This game does not support mouse steering by default, however there are instructions on how to configure this function by creating a virtual joystick and binding it to mouse and keyboard inputs. Unfortunately, those instructions are written for Windows and require two pieces of software that aren’t readily available on Linux – vJoy and FreePIE.
We need 1) to read mouse output, specifically x and y axes values and 2) create a virtual joystick to adapt said mouse output to joystick input. A brief search online revealed the evdev interface that “serves the purpose of passing events generated in the kernel directly to userspace through character devices” (https://python-evdev.readthedocs.io/en/latest/index.html). The python-evdev package is a convenient Python wrapper around the interface which can be installed with pip3 install python-evdev
. The second piece of the puzzle is python-uinput package which is the “interface to Linux uinput kernel module which allows attaching userspace device drivers into kernel” (https://github.com/pyinput/python-uinput); it can be installed with pip3 install python-uinput
.
Below is the final script and explanation.
import evdev
import uinput
RANGE = 65536 # 0 - 65535
HALF_RANGE = RANGE