gvalkov / python-evdev

Python bindings for the Linux input subsystem
https://python-evdev.rtfd.org/
BSD 3-Clause "New" or "Revised" License
331 stars 109 forks source link

Question about emulating touch events #158

Open makeevolution opened 3 years ago

makeevolution commented 3 years ago

I saw in the injecting input section of the documentation that the library "accepts only KEY_* events by default". Does this mean I cannot inject touch events (like touching, swiping, etc.), and if I am wrong, how would I go about injecting such an touch event?

LinusCDE commented 3 years ago

With "by default", I think they mean that when creating a device, they say to support Keyboard events since it's the most common use case. Evdev devices usually specify all the keys they'll ever send beforehand to allow other programs to classify them by. You can simply specify your own events=.

Here is an example I did that basically proxied Wacom touch events from an embedded device to a host computer: https://github.com/LinusCDE/rmTabletDriver/blob/738294772d613ab42478db98ded2faf3d68f8c47/tabletDriver.py#L43

I don't think that such a thing as a "swipe" event exists within evdev. You probably need to fake the swipe by sending the fitting touch events. You should also consider what type of "touch events" you want to send.

The best thing is probably to look at the emitted events of the device type you want to fake (using evtest). Mutitouch (MT) for example usually increases the ABS_MT_TRACKING_ID and keeps track of fingers by changing the ABS_MT_SLOT before each X, Y coordinate events.

Here are some example codes uses for the reMarkable tablet (an embedded eink device aimed at notetaking). The marked lines are a typical Multitouch screen (like on smartphones). The codes and names should match with the ecodes of python-evdev. https://github.com/canselcik/libremarkable/blob/master/src/input/ecodes.rs#L13-L21