使用python uvdev设置绝对光标位置

时间:2019-12-24 12:00:59

标签: python linux evdev

我正在尝试在python中模拟图形输入板,因此,我需要能够设置光标的绝对位置。我已经尝试过python-evdevpython-libevdev,但是无法设置绝对位置。将EV_ABS ABS_X和ABS_Y的拧紧值根本不会对光标位置产生任何影响。值得一提的是,模拟按钮和相对定位效果完美。

我正在x11上使用Manjaro 4.19和Gnome。

我将不胜感激,在此先感谢您。

这是简单的代码,必须能够设置绝对光标位置,但不是。

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())

1 个答案:

答案 0 :(得分:0)

好的。我认为要解决此问题,您需要将BTN_TOOL_PEN添加到EV_KEY。至少这对我有用。如果您有兴趣,可以在my github上查看整个项目。

相关问题