admin管理员组文章数量:1609936
按键精灵想必很多人都玩过,使用录制功能将鼠标和键盘的操作录制下来,录制好后就可以通过回放自动执行之前录制的操作,可以设置重复执行的次数,这样就可以将一些重复的劳动交给脚本自动化去完成。使用Python编写一个和脚本精灵一样的程序非常简单,并且代码量足够少,好处是可以不再依赖按键精灵,像我这种有轻微洁癖的程序猿就喜欢自己动手实现,依赖Python的为所欲为的特性,可以任意编码让自己的按键精灵更加强大。
按键精灵的实现可以拆解分为录制和回放两个步骤,对应到Python程序的实现也可以分为两步:
1.监听鼠标键盘的事件和坐标,写入到文件中记录起来。
2.读取监听时写入的文件,执行文件中的坐标和事件操作。
要实现这两个功能,得先从基础开始。我把任务拆解成两大部分,鼠标和键盘属于不同的两个输入设备,所以分开实现“鼠标精灵”和“键盘精灵”两个程序,最后融合这两个模块实现一个相对完整的按键精灵。
鼠标事件监听from pynput import mouse
# 鼠标移动事件
def on_move(x, y):
print('[Move]', (x, y))
# 鼠标点击事件
def on_click(x, y, button, pressed):
print('[Click]', (x, y, button.name, pressed))
# 鼠标滚动事件
def on_scroll(x, y, x_axis, y_axis):
print('[Scroll]', (x, y, x_axis, y_axis))
# 监听事件绑定
with mouse.Listener(on_move=on_move, on_click=on_click, on_scroll=on_scroll) as listener:
listener.join()
onMove(x,y)函数接收鼠标当前的x轴和y轴坐标,启动程序并移动鼠标时,就会调用该方法。
on_click(x, y, button, pressed)函数接收鼠标的点击事件,x和y为当前点击事件的鼠标坐标,button参数对象的name属性值为left或者right,通过该属性值可以判断是鼠标的左键还是右键产生的点击事件,pressed参数值为True时表示当前鼠标左或右键按压,False时表示鼠标左或右键抬起事件。
on_scroll(x, y, x_axis, y_axis)接收四个参数,前两个参数依旧是当前事件的鼠标坐标轴,x_axis的值>0表示向上,<0表示向下,同样的y_axis的负值和正值代表左滑和右滑状态。
鼠标事件执行from pynput.mouse import Button, Controller
import time
# 获取鼠标对象
mouse = Controller()
# 输出鼠标当前的坐标
print(mouse.position)
# 将新的坐标赋值给鼠标对象
mouse.position = (100, 500)
for index in range(0, 30):
# 鼠标移动到指定坐标轴
mouse.move(index, -index)
print(mouse.position)
time.sleep(0.01)
for index in range(0, 30):
# 鼠标移动到指定坐标轴
mouse.move(-index, index)
print(mouse.position)
time.sleep(0.01)
# 鼠标右键按下
mouse.press(Button.right)
time.sleep(0.01)
# 鼠标右键抬起
mouse.release(Button.right)
# 鼠标左键点击
mouse.click(Button.left, 1)
# 鼠标滚轮滚动距离500
mouse.scroll(0, 500)
和鼠标事件监听一样,对应的我们可以操作鼠标的各种事件:移动、左/右按压、左/右抬起、左/右点击、上下左右滚动
上面代码中的mouse.move(x,y)函数,表示从当前鼠标位置进行位移的距离,x和y的值是以当前位置为0点开始算的。
而且不能简单的用坐标轴去相减得到位移距离,所以后续的程序我会使用mouse.position = (x, y)这个函数来操作鼠标的移动,
这个函数可以将鼠标设置到指定位置,只要我们记录之前的鼠标移动轨迹,就可以通过读取之前的记录文件按顺序重新对鼠标进行赋值操作。达到回放的效果。
带录制回放功能的鼠标精灵
结合鼠标事件的监听和执行,并且将事件记录到文件中,再加上Python自带的GUI,就可以写出一个简单的鼠标精灵了。
实现思路:定义一个json格式的对象来统一不同鼠标事件的内容格式{
"name":"mouse",
"event":"click",
"target":"left",
"action":true,
"location":{
"x":"0",
"y":"0"
}
}
鼠标事件的name值为mouse,考虑到后续还有可能会有其他设备的事件,比如键盘事件。
鼠标的事件为点击事件,将event赋值为click。
target表示目标,点击了鼠标左键,所以目标值为left
action表示动作,鼠标点击分为按压和抬起,true表示抬起。
location的值包含一个json对象,里面为当前事件鼠标的x和y坐标。
鼠标的移动和滑动事件以此类推,用同样的模板格式进行记录。将记录的数据写入到文件中
执行回放时通过逐行读取文件,解析文件中的json数据通过name、event、location等值进行事件执行。
之所以选择json时因为解析起来比较方便。当然你也可以使用数据库,比如SQLite等,或者自己定义一套自己的格式。
json的存储方式有太多的冗余字符了,空间占用目前不是考虑的首要因素,这里先用最快的方式实现它。
GUI用了Python自带的tkinter库,为了防止UI阻塞,所以使用了多线程,同样是Python自带库threading。
整个实现其实只用了一个三方库 pynput
鼠标事件的监听录制和执行回放的完整代码如下:import json
import threading
import time
import tkinter
from pynput import mouse
from pynput.mouse import Button, Controller
# 鼠标动作模板
def mouse_action_template():
return {
"name": "mouse",
"event": "default",
"target": "default",
"action": "default",
"location": {
"x": "0",
"y": "0"
}
}
# 鼠标动作监听
class MouseActionListener(threading.Thread):
def __init__(self, file_name):
super().__init__()
self.file_name = file_name
def run(self):
with open(self.file_name, 'w', encoding='utf-8') as file:
# 鼠标移动事件
def on_move(x, y):
template = mouse_action_template()
template['event'] = 'move'
template['location']['x'] = x
template['location']['y'] = y
file.writelines(json.dumps(template) + "\n")
file.flush()
# 鼠标点击事件
def on_click(x, y, button, pressed):
template = mouse_action_template()
template['event'] = 'click'
template['target'] = button.name
template['action'] = pressed
template['location']['x'] = x
template['location']['y'] = y
file.writelines(json.dumps(template) + "\n")
file.flush()
# 鼠标滚动事件
def on_scroll(x, y, x_axis, y_axis):
template = mouse_action_template()
template['event'] = 'scroll'
<版权声明:本文标题:python 按键精灵脚本_[620]使用Python实现一个按键精灵 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1728584816a1164845.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论