使用 Python 创建自动点击器
顾名思义,Python 中的自动点击器是一个简单的 Python 应用程序,它根据用户要求重复单击鼠标。 不同的参数,如速度、频率和位置,可以根据用户进行更改。
Python 有不同的模块可用于控制键盘、鼠标等设备。因此,我们可以使用这些模块轻松地在 Python 中创建自动点击器。
本篇文章将介绍在 Python 中创建自动答题器的不同方法。
使用 pyautogui 模块在 Python 中创建自动点击器
pyautogui 模块可以创建控制设备的鼠标和键盘的脚本。
我们可以使用 pyautogui.click()
函数来单击鼠标。 我们可以使用 pyautogui.moveTo()
函数预先将鼠标移动到所需位置,并指定光标的坐标。
要使用这些函数创建自动点击器,我们将移动鼠标并运行 for 循环来执行每次迭代的 pyautogui.click()
函数。 我们还会在每次点击之前稍微延迟以使其可见。
我们将使用 time 模块中的 sleep()
函数来进行延迟。
我们在下面的代码中实现自动点击器的逻辑。
import pyautogui
import time
pyautogui.moveTo(600, 800)
for i in range(20):
time.sleep(0.1)
pyautogui.click()
在上面的示例中,我们将光标移动到坐标(600,800)并使用 click()
函数单击鼠标 20 次。
使用 win32api 在 Python 中创建自动点击器
win32api 用于控制和自动化 Windows COM 对象。 我们可以使用 win32con 模块中的这些对象并模拟鼠标单击。
为此,我们使用 win32api.mouse_event 函数两次,一次用于 win32con.MOUSEEVENTF_LEFTDOWN,另一次用于 win32con.MOUSEEVENTF_LEFTDOWN 来模拟鼠标单击。
我们将使用 win23api.SetCursorPos()
函数将光标的位置设置为所需的坐标。 然后,我们将使用与之前方法类似的方法在 Python 中创建自动点击器。
请参阅下面的代码。
import win32api, win32con
import time
win32api.SetCursorPos((600,800))
for i in range(20):
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,600,800,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,600,800,0,0)
time.sleep(0.1)
使用 pynput 模块在 Python 中创建自动点击器 我们还有 pynput 用于在 Python 脚本中自动执行鼠标和键盘操作。 此方法与前两种方法不同,因为我们将创建一个具有创建自动点击器所需功能的类。
我们将创建一个类,当按下特定键时,它将充当自动点击器。 将分配另一个键来停止点击。
我们需要导入 pynput.mouse 和 pynput.keyboard 模块。
创建的类将扩展为threading.Thread类,以便我们可以控制具有不同功能的线程。
在类的 __init__
方法中,我们将像之前一样指定每次单击之间的轻微延迟,并提到我们将使用 Button.Left 对象单击鼠标左键。
线程将继续运行,直到按下停止点击的键为止。 我们将使用 threading.Thread 类的 start()
方法来开始单击。
在 running()
方法中,我们还将使用 Controller 类对象。
为了启动和停止线程,我们将创建一个函数。 Listener()
函数将在使用 on_press 参数按下按键时执行定义的函数。
这个逻辑在下面的代码中实现。
import time
import threading
from pynput.mouse import Button, Controller
from pynput.keyboard import Listener, KeyCode
start_end = KeyCode(char='a')
exit_key = KeyCode(char='z')
class auto_clicker_class(threading.Thread):
def __init__(self):
super(auto_clicker_class, self).__init__()
self.delay = 0.1
self.button = Button.left
self.running = False
self.program_run = True
def begin_clicking(self):
self.running = True
def clicking_stop(self):
self.running = False
def exit(self):
self.clicking_stop()
self.program_run = False
def run(self):
while self.program_run:
while self.running:
mouse_ob.click(self.button)
time.sleep(self.delay)
time.sleep(0.1)
mouse_ob = Controller()
t = auto_clicker_class()
t.start()
def fun(k):
if k == start_end:
if t.running:
t.clicking_stop()
else:
t.begin_clicking()
elif k == exit_key:
t.exit()
listener.stop()
with Listener(on_press=fun) as listener:
listener.join()
总姐
我们在本篇文章中讨论了自动答题器。 前两种方法相对简单,因为我们使用 for 循环重复执行一个函数多次来模拟鼠标单击。
最后一种方法在 Python 中创建一个适当的自动点击器脚本,可以根据键盘上按下的键开始和停止点击。
相关文章
Pandas DataFrame DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
Pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:Python
-
Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。
Pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:Python
-
本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。
Pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:Python
-
本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串