使用pynput监听鼠标,并直接生成代码:
from pynput import mouse
from pynput.mouse import Button
import datetime
with open('/home/oliveryty/文档/监听鼠标.txt', 'a+') as f:
f.write("from pynput.mouse import Button, Controller as mCtr")
f.write("\n\n")
f.write("import time")
f.write("\n\n")
f.write("mouse = mCtr()")
f.write("\n\n")
def on_move(x, y):
return 0
def on_click(x, y, button, pressed):
with open('/home/oliveryty/文档/监听鼠标.txt', 'a+') as f:
if button == Button.left:
f.write("\n")
f.write(f'mouse.position = {(x, y)}')
f.write("\n")
f.write(f'{"mouse.press(Button.left)" if pressed else "mouse.release(Button.left)"}')
f.write("\n")
f.write("time.sleep(1)")
f.write("\n")
elif button == Button.right:
f.write("\n")
f.write(f'mouse.position = {(x, y)}')
f.write("\n")
f.write(f'{"mouse.press(Button.right)" if pressed else "mouse.release(Button.right)"}')
f.write("\n")
f.write("time.sleep(1)")
f.write("\n")
def on_scroll(x, y, dx, dy):
with open('/home/oliveryty/文档/监听鼠标.txt', 'a+') as f:
f.write("\n")
f.write(f'mouse.position = {(x, y)}')
f.write("\n")
f.write(f'mouse.scroll{(dx, dy)}')
f.write("\n")
f.write("time.sleep(1)")
f.write("\n")
with mouse.Listener(
on_move = on_move,
on_click = on_click,
on_scroll = on_scroll) as listener:
listener.join()
执行上述代码,pynput将监听鼠标的点击动作和转动滚轴动作,作为日志记录到txt文本中。而生成的日志就是python代码。这样,将txt后缀改成py后,就可以直接执行了!!
下述代码就是上述代码自动生成的。
from pynput.mouse import Button, Controller as mCtr
import time
mouse = mCtr()
mouse.position = (412, 417)
mouse.press(Button.left)
time.sleep(1)
mouse.position = (412, 417)
mouse.release(Button.left)
time.sleep(1)
mouse.position = (414, 425)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (414, 430)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (414, 436)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (414, 437)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (414, 440)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (414, 442)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (239, 752)
mouse.press(Button.left)
time.sleep(1)
mouse.position = (239, 752)
mouse.release(Button.left)
time.sleep(1)
mouse.position = (269, 212)
mouse.press(Button.left)
time.sleep(1)
mouse.position = (269, 212)
mouse.release(Button.left)
time.sleep(1)
mouse.position = (375, 60)
mouse.press(Button.left)
time.sleep(1)
mouse.position = (375, 60)
mouse.release(Button.left)
time.sleep(1)
mouse.position = (395, 82)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (409, 110)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (528, 336)
mouse.press(Button.left)
time.sleep(1)
mouse.position = (528, 336)
mouse.release(Button.left)
time.sleep(1)
mouse.position = (781, 407)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (781, 407)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (781, 410)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (781, 414)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (780, 417)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (777, 421)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (776, 422)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (775, 422)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (775, 422)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (775, 422)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (775, 422)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (775, 422)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (775, 422)
mouse.scroll(0, 1)
time.sleep(1)
mouse.position = (775, 422)
mouse.scroll(0, 1)
time.sleep(1)
mouse.position = (775, 422)
mouse.scroll(0, 1)
time.sleep(1)
mouse.position = (775, 422)
mouse.scroll(0, 1)
time.sleep(1)
mouse.position = (775, 422)
mouse.scroll(0, 1)
time.sleep(1)
mouse.position = (775, 422)
mouse.scroll(0, 1)
time.sleep(1)
mouse.position = (265, 15)
mouse.press(Button.left)
time.sleep(1)
mouse.position = (265, 15)
mouse.release(Button.left)
time.sleep(1)
#记录到了右键
mouse.position = (356, 239)
mouse.press(Button.right)
time.sleep(1)
mouse.position = (356, 239)
mouse.release(Button.right)
time.sleep(1)
mouse.position = (426, 496)
mouse.press(Button.left)
time.sleep(1)
mouse.position = (426, 496)
mouse.release(Button.left)
time.sleep(1)
mouse.position = (467, 13)
mouse.press(Button.left)
time.sleep(1)
mouse.position = (467, 13)
mouse.release(Button.left)
time.sleep(1)
mouse.position = (397, 759)
mouse.press(Button.left)
time.sleep(1)
mouse.position = (397, 759)
mouse.release(Button.left)
time.sleep(1)
mouse.position = (541, 422)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (541, 424)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (541, 424)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (541, 424)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (573, 394)
mouse.scroll(0, 1)
time.sleep(1)
mouse.position = (573, 392)
mouse.scroll(0, 1)
time.sleep(1)
mouse.position = (573, 392)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (573, 392)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (573, 392)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (573, 392)
mouse.scroll(0, -1)
time.sleep(1)
mouse.position = (1036, 18)
mouse.press(Button.left)
time.sleep(1)
mouse.position = (1036, 18)
mouse.release(Button.left)
time.sleep(1)
mouse.position = (610, 459)
mouse.press(Button.left)
time.sleep(1)
mouse.position = (610, 459)
mouse.release(Button.left)
time.sleep(1)
之前学习pynput操控鼠标,光定位mouse.position就费老鼻子的事了。有了这个法子,直接生成代码,就问你服不服。
大家如果有简单的自动化需求,可以直接复制监听代码,在安装了python3环境并安装了pynput模块的情况下,直接执行。注意把txt文件的保存目录改成D盘,因为我是在fedora linux下运行的。即:with open('/home/oliveryty/文档/监听鼠标.txt', 'a+') as f:改成with open('D:/监听鼠标.txt', 'a+') as f: 。
当然,这里其实只生成了鼠标左右键的操作和转动滚轴的操作。另外转动滚轴,每次只记录一下,真正做成自动化程序的时候,可以把mouse.scroll(dx, dy)的dy值改成较大数值,方便看,也不用受限于time.sleep(1)造成的比实际操作慢的效果了。如果编写同时操控键盘的程序时,可以自行手工添加代码,并函数化或类化。