python - 使用 python uvdev 设置绝对光标位置
问题描述
我正在尝试在 python 中模拟图形输入板,因此,我需要能够设置光标的绝对位置。我试过python-evdev和python-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())
解决方案
好的。我认为要解决此问题,您需要将 BTN_TOOL_PEN 添加到 EV_KEY。至少这对我有用。如果您有兴趣,可以在我的 github上查看整个项目。
推荐阅读
- java - 写一个静态方法来找成绩
- python - 如何汇总与使用 Pandas 创建的每个组相关的一系列数据?
- javascript - 按钮在列表组内不起作用
- jquery - 在 jquery 中插入 html 代码并给出 html 语法问题
- javascript - 如何引用推入数组的随机生成的 x 和 y 值
- laravel - Laravel 双重身份验证和@guest 声明
- r - 逻辑回归 R 中的分类独立 - 我如何理解整体效果?
- ruby-on-rails - 如何从 ruby 中的哈希嵌套数组中获取数据?
- python - 无法让 python split() 与 £ 一起工作
- typescript - 类表达式的显式类型