简体   繁体   中英

Setting absolute cursor position using python uvdev

I'm trying to simulate a graphics tablet in python, and as a result, I need to be able to set the absolute position of the cursor. I have tried python-evdev and python-libevdev , but I'm unable to set the absolute position. Wring values to the EV_ABS ABS_X and ABS_Y simply doesn't take any effect on a cursor position. It worth mentioning that simulated buttons and relative positioning work perfectly.

I'm using Manjaro 4.19 with Gnome on x11.

I would appreciate any help and thank you in advance.

Here's simple code which must be able to set the absolute cursor position, but it's not.

from evdev import UInput, AbsInfo, ecodes as e
import pyautogui
import subprocess
import time

cap = {
    e.EV_KEY : [e.BTN_TOUCH],
    e.EV_ABS : [
        (e.ABS_X, AbsInfo(0, 0, 4080, 0, 0, 5080)),         
        (e.ABS_Y, AbsInfo(0, 0, 4080, 0, 0, 5080)),
        (e.ABS_PRESSURE, AbsInfo(0, 0, 2040, 0, 0, 0))
    ]
}

ui = UInput(cap, name='example-device', version=0x3)

for i in range(0,10):
    # assign driver to the default display
    time.sleep(0.5)
    process = subprocess.Popen(["xinput", "list"], shell=True, stdout=subprocess.PIPE)
    output = process.stdout.read().decode("utf-8")
    print(output)
    if "example-device" in output:
        print(output)
        subprocess.Popen([
            "xinput",
            "map-to-output",
            f"pointer:example-device",
            "eDP1"
        ])
        break
else:
    raise Exception("Can't map device to display.")

# print cursor position befor cahnge
print(pyautogui.position())

# move mouse cursor
ui.write(e.EV_ABS, e.ABS_X, 20)
ui.write(e.EV_ABS, e.ABS_Y, 20)
ui.syn()
# print cursor position after cahnge
print(pyautogui.position())

Ok. I think to solve this you need to add BTN_TOOL_PEN to EV_KEY. At least this is worked for me. If you are interested you can see the whole project on my github .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM