此点击器主要依赖python的pyautogui模块,可以完成对鼠标位置的的拾取,左键单击、左键双击、右键单击、鼠标下滑、鼠标上滑等基本操作。可以自动获取config.ini配置文件并根据用户的输入完成多次鼠标循环操作。
核心点击代码如下:
def run(second):#执行点击,传入形参second
config.read('config.ini')
num = 1
while True:
run_stop()#监听键盘事件,判断是否需要停止
x_pos = 'x' + str(num)
y_pos = 'y' + str(num)
c_pos = 'c' + str(num)
findstr_x = 'x' + str(num)
findstr_y = 'y' + str(num)
findstr_c = 'c' + str(num)
if config.has_option('database',findstr_x):#config.has_option('section_name', 'config_value')
x_pos = config['database'][findstr_x]
y_pos = config['database'][findstr_y]
c_pos = config['database'][findstr_c]
pyautogui.moveTo(int(x_pos),int(y_pos),duration=float(second)) #将字符强转成int类型,duration=2为两秒内完成
print("找到第" + str(num) + "个点")
run_stop()#监听键盘事件,判断是否需要停止
select_act(int(c_pos))#调用方法
num +=1
else:
break
动作执行代码:
def select_act(c):#执行相应的动作1、鼠标左键单击;2、鼠标左键双击、3、鼠标右键单机、鼠标右键双击
print("执行动作" + str(c))
if int(c) == 1:#因为读取的config是字符串的形式,所以需要转换成int类型去对比
pyautogui.click()
elif int(c) == 2:
pyautogui.doubleClick()
elif int(c) == 3:
pyautogui.click(button='right')
elif int(c) == 4:
pyautogui.scroll(-10000)#向下滚动
elif int(c) == 5:
pyautogui.scroll(10000)#向上滚动
else:
pyautogui.click()
以下为全部代码,复制粘贴即可运行,需要安装必要模块
pip install pyautogui
pip install keyboard
import pyautogui
import keyboard
import configparser
import time,sys
pyautogui.FAILSAFE=False#关闭自动保护
'''
1、用户权限问题(已解决)
2、将点击的定值改成动态(已解决)
3、如果是管理员权限启动的桌面程序,在电击器没有使用管理员权限点击器则无法点击(已解决)
'''
def get_admin():#获取管理员权限
# 如果当前脚本以管理员权限运行,则直接返回
if ctypes.windll.shell32.IsUserAnAdmin():
return True
else:
# 否则,使用PowerShell命令以管理员权限启动当前脚本
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
sys.exit()
def menu():
print("1、按a拾取鼠标位置(写入config.ini)")
print("2、按b获取屏幕分辨率")
print("3、按r进入自动点击(读取config.ini)")
print("4、长按[ctrl + 1]可选择退出正在进行的循环")
print("5、按ese退出")
print("========================")
pyautogui.alert(text ="1、按a拾取鼠标位置(写入config.ini)" +"\n" +
"2、按b获取屏幕分辨率" + "\n" +
"3、按r进入自动点击(读取config.ini)" + "\n" +
"4、长按[ctrl + 1]可选择退出正在进行的循环" + "\n" +
"5、按ese退出",title='自动点击器')
def sava_pos(x,y,n):#保存获得的坐标到指定的文档
print(x,y)
#n = 1
x_write = 'x' + str(n)
y_write = 'y' + str(n)
c_write = 'c' + str(n)
with open('config.ini', 'a+') as configfile:
configfile.write(x_write + '= '+ str(x) + "\n")
configfile.write(y_write + '= '+ str(y) + "\n")
configfile.write(c_write + "=" + str(sava_act(n)) + "\n")
def sava_act(n):#保存动作
#print("请选择相应的动作1、鼠标左键单击;2、鼠标左键双击")
print("动作标记")
confirm = pyautogui.confirm(text ="1、鼠标左键单击" + "\n" +
"2、鼠标左键双击" + "\n" +
"3、鼠标右键单击" + "\n" +
"4、鼠标向下滚动" + "\n" +
"5、鼠标向上滚动",
buttons=['1','2','3','4','5'],
title="请选择第" + str(n) + "个位置的动作")
print(confirm)
if confirm == '1':
return 1
elif confirm == '2':
return 2
elif confirm == '3':
return 3
elif confirm == '4':
return 4
elif confirm == '5':
return 5
else:
return 1
def get_size():#获取屏幕分辨率
print("获取当前屏幕分辨率:")
x,y = pyautogui.size()
print(x,y)
pyautogui.alert(text =(str(x) + "*" + str(y)),title='当前屏幕分辨率')
def select_act(c):#执行相应的动作1、鼠标左键单击;2、鼠标左键双击、3、鼠标右键单机、鼠标右键双击
print("执行动作" + str(c))
if int(c) == 1:#因为读取的config是字符串的形式,所以需要转换成int类型去对比
pyautogui.click()
elif int(c) == 2:
pyautogui.doubleClick()
elif int(c) == 3:
pyautogui.click(button='right')
elif int(c) == 4:
pyautogui.scroll(-10000)#向下滚动
elif int(c) == 5:
pyautogui.scroll(10000)#向上滚动
else:
pyautogui.click()
def run_more():
times = pyautogui.prompt("请输入需要点击的轮次",title='提示')
print("需要点击的轮次:" + str(times))
second = pyautogui.prompt("请输入时间间隔,单位为秒",title='提示')
if times != None and second != None:
temp_num = 1
while True:
run(second)
print("已完成第" + str(temp_num) + "轮")
temp_num += 1
if temp_num > int(times):
print("已全部执行")
pyautogui.alert(text ="已完成" + str(times) + "轮点击!",title='提示')
break
else:
pyautogui.alert(text ="输入值为空,点击不执行",title='提示')
def run_stop():#停止正在进行的循环
if keyboard.is_pressed('ctrl + 1'):
confirm = pyautogui.confirm(text ="确定要停止吗!",title='提示')
if confirm == 'OK':
print("已停止")
sys.exit()
def run(second):#执行点击,传入形参second
config.read('config.ini')
num = 1
while True:
run_stop()#监听键盘事件,判断是否需要停止
x_pos = 'x' + str(num)
y_pos = 'y' + str(num)
c_pos = 'c' + str(num)
findstr_x = 'x' + str(num)
findstr_y = 'y' + str(num)
findstr_c = 'c' + str(num)
if config.has_option('database',findstr_x):#config.has_option('section_name', 'config_value')
x_pos = config['database'][findstr_x]
y_pos = config['database'][findstr_y]
c_pos = config['database'][findstr_c]
pyautogui.moveTo(int(x_pos),int(y_pos),duration=float(second)) #将字符强转成int类型,duration=2为两秒内完成
print("找到第" + str(num) + "个点")
run_stop()#监听键盘事件,判断是否需要停止
select_act(int(c_pos))#调用方法
num +=1
else:
break
def sava_config():#保存config文件
config['database'] = {}
with open('config.ini', 'w') as configfile:
config.write(configfile)
def on_key(event):#监听相应的按钮操作
if event.name == "a":
pyautogui.alert(text ="进入位置拾取,请根据提示进行位置拾取!",title='提示')
n = 1
while True:
if n == 1:
sava_config()
confirm = pyautogui.confirm(text ="点击确定后,请在3秒内将鼠标定位到第" + str(n) + "个位置,点击取消则中断拾取",title='提示')
if confirm == 'Cancel':
print("结束拾取")
confirm = pyautogui.confirm(text ="结束拾取",title='提示')
break
time.sleep(3) #延迟3秒
print("已拾取第" + str(n) + "个位置")
x,y = pyautogui.position()
sava_pos(x,y,n)
n += 1
if event.name == "b":
get_size()
if event.name == "r":
run_more()
if __name__ == "__main__":
menu()
config = configparser.ConfigParser()
keyboard.on_press(on_key)
keyboard.wait('esc')