在 python 中创建一个基本的自动点击器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42877783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:14:00  来源:igfitidea点击:

Creating a basic auto clicker in python

python

提问by Jmilicev

I want to create a pretty simple automatic-clicker that will take the position of your mouse, and clicks in that position as long as "active" is true and at "input" 's speed (clicks per second)

我想创建一个非常简单的自动点击器,它将占据鼠标的位置,只要“活动”为真并以“输入”的速度(每秒点击次数)点击该位置

I have seen this code floating around, but it does not suit my needs

我看到这段代码四处飘荡,但它不适合我的需要

import win32api, win32con
def click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
click(10,10)

I would like the code to look something like this,

我希望代码看起来像这样,

import library 

input = 5 ## in clicks per second

if (some key) is held:
    active = True

if (some key) is released:
    active = False


while active:
     *gets position of mouse*
     *clicks at that position at input's speed (5)

It would also be nice if there was an option to click in the very center of the screen.

如果有一个选项可以单击屏幕的正中央,那也很好。

回答by arsho

Try using pyautogui. I have used this in several projects. It has huge functionalities with less coding. For example, if you want to click on the middle of the screen, simply do:

尝试使用pyautogui. 我已经在几个项目中使用过它。它具有巨大的功能和更少的编码。例如,如果您想单击屏幕中间,只需执行以下操作:

import pyautogui
width, height = pyautogui.size()
pyautogui.click(width/2, height/2)

In your case you may use time module to synchronize the actions.

在您的情况下,您可以使用时间模块来同步操作。

Here is the cheat sheet of pyautogui.

这是pyautogui备忘单