在Debian上自动化Wayland中的鼠标指针移动(非X11/X窗口系统)

huangapple go评论58阅读模式
英文:

Automate mouse pointer motion in Wayland on Debian (Not X11/X Window System)

问题

I am trying to find a way to simulate/automate mouse motion using the Wayland protocol on a debian based OS as did using Xlib in X11/X Window System giving the x and y coordinates:

from Xlib.display import Display
from Xlib.ext.xtest import fake_input
from Xlib import X
import os

#Go to x and y coordinates and click with left button (1):
x = 26
y = 665
button = 1

myDisplay = Display(os.environ['DISPLAY'])

fake_input(myDisplay, X.MotionNotify, x=x, y=y)
myDisplay.sync()
fake_input(myDisplay, X.ButtonPress, button)
myDisplay.sync()
fake_input(myDisplay, X.ButtonRelease, button)
myDisplay.sync()

and with pynput:

from pynput.mouse import Button, Controller

mouse = Controller()

mouse.position = (26,665)

mouse.click(Button.left)

I have tried with uinput to reproduce the same idea from the scripts presented above, the code doesn't give error but nothing happens when the script is executed:

sudo modprobe uinput && sudo python3 uinputtest.py

import uinput

# Create new mouse device
device = uinput.Device([
    uinput.BTN_LEFT,
    uinput.BTN_RIGHT,
    uinput.REL_X,
    uinput.REL_Y,
])

# Move the pointer to x = 26 and y = 665
device.emit(uinput.REL_X, 26)
device.emit(uinput.REL_Y, 665)

# Click and release the left mouse button 
device.emit(uinput.BTN_LEFT, 1)
device.emit(uinput.BTN_LEFT, 0)

Is there anything missing on this last script? I am trying to execute it on Wayland with super user permission.

英文:

I am trying to find a way to simulate/automate mouse motion using the Wayland protocol on a debian based OS as did using Xlib in X11/X Window System giving the x and y coordinates:

from Xlib.display import Display
from Xlib.ext.xtest import fake_input
from Xlib import X
import os

#Go to x and y coordinates and click with left button (1):
x = 26
y = 665
button = 1

myDisplay = Display(os.environ['DISPLAY'])

fake_input(myDisplay, X.MotionNotify, x=x, y=y)
myDisplay.sync()
fake_input(myDisplay, X.ButtonPress, button)
myDisplay.sync()
fake_input(myDisplay, X.ButtonRelease, button)
myDisplay.sync()

and with pynput:

from pynput.mouse import Button, Controller

mouse = Controller()

mouse.position = (26,665)

mouse.click(Button.left)

I have tried with uinput to reproduce the same idea from the scripts presented above, the code doesn't give error but nothing happens when the script is executed:

sudo modprobe uinput && sudo python3 uinputtest.py

import uinput

# Create new mouse device
device = uinput.Device([
    uinput.BTN_LEFT,
    uinput.BTN_RIGHT,
    uinput.REL_X,
    uinput.REL_Y,
])

# Move the pointer to x = 26 and y = 665
device.emit(uinput.REL_X, 26)
device.emit(uinput.REL_Y, 665)

# Click and release the left mouse button 
device.emit(uinput.BTN_LEFT, 1)
device.emit(uinput.BTN_LEFT, 0)

Is there anything missing on this last script? I am trying to execute it on Wayland with super user permission.

答案1

得分: 2

在uinput的示例代码中提到:
> 我们在这里插入一个暂停,以便用户空间有足够的时间来检测、初始化新设备,并开始监听事件,否则它将不会注意到我们即将发送的事件。这个暂停只在我们的示例代码中需要!

在设备初始化后加入 time.sleep(1) 可以解决这个问题。
Ydotool 也提供了一个用户空间应用程序和守护进程,用于实现您想要实现的目标,并等待一秒。

英文:

You should wait a bit before emitting any event.

In uinput example code it says:
> We are inserting a pause here so that userspace has time
to detect, initialize the new device, and can start listening to
the event, otherwise it will not notice the event we are about
to send. This pause is only needed in our example code!

Putting a time.sleep(1) after the device intialization fixes the problem.
Ydotool, which provides a userspace application and daemon for what you are trying to achieve also waits for a second.

huangapple
  • 本文由 发表于 2023年2月14日 03:16:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定