这篇可能是你见过最全的鼠标键盘监测
import time
from pynput.mouse import Button
from pynput.keyboard import Controller, Key
from pynput.keyboard import Listener as Keyboard_Listener
from pynput.mouse import Listener as Mouse_Listener
import os
from threading import Thread
#用于区分单双击
click_location=[]
time_difference=0
click_count=1
click_time=[]
path_clicked={}
#用于存放监听命令
##global command_list
command_list=[]
#表头为输入函数库
title ='\n'+ 'import pyautogui'+'\n' + 'import time' + '\n' +'\n'
def on_click(x, y , button, pressed):
if pressed: #点击时为ture 如果不进行判断会调用两次这个函数 一次点击一次释放 这里不需要两次
global click_count
global time_difference #把时间差设为全局变量 为的是退出这个循环
t=time.time() #获取当前电脑时间
click_time.append(t) #添加时间
click_location.append((x,y)) #添加位置
if len(click_location)!=1: #这几个判断以及上面定义的click_ 都是为了得到双击还是单击 两个list长度都是2 第一个和第二个比较时间差
time_difference=click_time[1]-click_time[0] #定义时间差
if click_location[0]==click_location[1]:
if time_difference<=0.3: #如果两次点击时间小于0.3秒就会判断为双击 否则就是单击
click_count=2
else:
click_count=1
else:
click_count=1
click_time.pop(0) #删去第一个
click_location.pop(0)
if button == Button.left: #判断左键还是右键还是中键
button_name = 'Button_left'
elif button == Button.middle:
button_name = 'Button_middle'
elif button == Button.right:
button_name = 'Button_right'
else:
button_name = 'Unknown'
if 'Key.esc' in all_key:
#在按下esc时将不再监控鼠标
return False
else:
if button_name == 'Button_left' and click_count == 1:
command = 'pyautogui.click{0}'.format((x,y))+'\n' + 'time.sleep(' + str(round(time_difference,2)) + ')' +'\n'
command_list.append(command)
elif button_name == 'Button_left' and click_count== 2:
command = 'pyautogui.doubleClick{0}'.format((x,y)) +'\n'+ 'time.sleep(' + str(round(time_difference,2)) + ')' +'\n'
command_list.pop(-1)
command_list.append(command)
elif button_name == 'Button_right' and click_count == 1:
command = 'pyautogui.rightClick{0}'.format((x,y))+'\n'+ 'time.sleep(' + str(round(time_difference,2)) + ')' +'\n'
command_list.append(command)
elif button_name == 'Button_middle' and click_count == 1:
command = 'pyautogui.click(button= \'middle\')'+'\n'+ 'time.sleep(' + str(round(time_difference,2)) + ')' +'\n'
command_list.append(command)
def on_scroll(x, y, dx, dy):
# 监听鼠标滚轮
if 'Key.esc' in all_key:
#在按下esc时将不再监控鼠标
return False
else:
command = 'pyautogui.scroll({0})'.format(dy*120) + '\n' #滚轮滚一下值为120
command_list.append(command)
print('pyautogui.scroll({0})'.format(dy*120))
def on_release(key):
all_key.append(str(key)) #用于在鼠标事件中监控是否按下esc
if len(str(key))>3: #用于区分按了字母键和功能键
keyword = '\'' + str(key)[4:] + '\''
else:
keyword = str(key)
command = keyword + '\n'#输出autogui可用语句
if key == Key.esc: #当按下esc停止监控
return False
else:
command_list.append(command)
print(command_list)
def text_create(name, msg):
desktop_path = os.path.abspath('.') # 新创建的txt文件的存放路径
full_path = desktop_path + '\\' + name + '~' + '.txt'
file = open(full_path, 'a')
file.write(msg)
file.close
def clear_txt(name): #清空内容
desktop_path = os.path.abspath('.') # 新创建的txt文件的存放路径
full_path = desktop_path + '\\' + name + '.txt'
global distinguish_py #主程序会用到
distinguish_py = desktop_path + '\\' + name + '.py'
if os.path.exists(full_path):
file = open(full_path, 'r+')
file.truncate()
file.close
def hot_key(name): #主要作用就是将组合键从单击字符形式转成hot_key模式
desktop_path = os.path.abspath('.') # 新创建的txt文件的存放路径
global full_path
full_path = desktop_path + '\\' + name +'~'+ '.txt' #过渡文件
replace_path = desktop_path + '\\' + name + '.txt' #最终文件
b = open(replace_path, 'a')
f = open(full_path, 'r+')
line = f.readline()
hotkey_list = []
func_key_list = ['ctrl', 'alt' , 'shift','cmd'] #功能按键
while line:
line = f.readline() #多读了一行,刚好把原来空行过掉
if line[:1]=='\'': #识别字母按键
for func_key in func_key_list:
if func_key in hotkey_list and func_key not in line: #当前一个为功能按键,后一个可能还是功能键可以也是字母键,此处只要字母键
command = 'pyautogui.hotkey(\'' + func_key + '\''+ ','+ line.strip() + ')' +'\n'
b.write(command)
hotkey_list = [] #清空为识别下一对组合按键,默认所有组合按键为两个按键
if func_key in line: #识别第一个功能按键
hotkey_list.append(func_key)
else:
b.write(line)
b.close
f.close
def change_suffix(name):
desktop_path = os.path.abspath('.') # 新创建的txt文件的存放路径
full_path = desktop_path + '\\' + name + '.txt'
portion = os.path.splitext(full_path)
if portion[1] == '.txt':
# 重新组合文件名和后缀名
newname = portion[0] + '.py'
os.rename(full_path, newname)
#################################################################################################################################
def mouse_click(x, y):
pass
def mouse_dclick(x, y):
pass
def mouse_drag():
(x1,y1)= on_click(x,y)
(x2,y2)= on_dclick(x, y)
if (x1y1) != (x2,y2):
command = 'pyautogui.click(941,34, button=\'left\')' + '\n' + 'pyautogui.dragRel(0,100, button=\'left\', duration=5)'
command_list.append(command)
print(command)
#功能待开发
#pyautogui.dragTo(x=427, y=535, duration=3,button='left')
#################################################################################################################################
if __name__=='__main__':
num = 0 #命名时用到
#最小化所有窗口待开发
export_file = 'record' #默认文件名
clear_txt(export_file) #清空txt
while os.path.isfile(distinguish_py):
num=int(num)+1
export_file = 'record'+ '(' + str(num) +')'
distinguish_py = os.path.abspath('.') + '\\' + export_file + '.py'#用于命名(1)
text_create(export_file, title) #先把引入函数写进去,此处title多留一行空白为后面生成最终文件用
global all_key #获取按键信息
all_key = []
global keyboard_listen_thread #分线程,两事件同时进行
global mouse_listen_thread
# 鼠标单独线程
mouse_listen_thread = Mouse_Listener(on_click=on_click, on_scroll=on_scroll)
mouse_listen_thread.start()
# 键盘单独线程
keyboard_listen_thread = Keyboard_Listener(on_release=on_release)
keyboard_listen_thread.start()
keyboard_listen_thread.join() #当按键进程结束后,鼠标进程也随之结束,后面才继续运行
for command in command_list:
text_create(export_file, command) #写入txt文件
hot_key(export_file) #此步骤为把热键整合
change_suffix(export_file) #更改后缀名
os.remove(full_path) #删除过渡文件.txt
print("All commands have been recorded!")
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: