首页 > 解决方案 > 如何在特定应用程序上模拟按键

问题描述

我正在尝试设置一个为我做事的机器人,目前我必须在同一个窗口中才能工作。当机器人在该特定应用程序上运行时,是否可以更改我的窗口(例如使用谷歌浏览器)?

此外,如果您询问它的应用程序,它的 Runescape 只是一个旧游戏,我只是用它来了解有关 python 的更多信息。

我在网上找不到任何关于这样做的模块的信息。

from pynput.mouse import Button, Controller
from time import sleep
from random import randint, uniform

def move_mouse(next_pos):
    sleep(uniform(0.1,0.2))
    mouse.position = (randint(int(round(next_pos[0])), int(round(next_pos[2]))), randint(int(round(next_pos[1])), int(round(next_pos[3]))))
    sleep(uniform(1,1.2))
    mouse.click(Button.left, 1)

mouse = Controller()
print(mouse.position)

shaft = [ 1299.25, 590.48828125, 1315.6171875, 610.69921875]
box = [705.01171875, 600.671875, 957.640625, 616.37109375]

sleep(2)
while True:
    move_mouse(shaft)
    move_mouse(box)
    sleep(71)

到目前为止,一切正常,我没有遇到任何错误。我所需要的只是让它点击一个特定的应用程序。

标签: pythonpynput

解决方案


推荐阅读