一、安装 pymouse
版本介绍:python 版本 3.7.6,pymouse==pymouse-1.0,pyHook==pyHook-1.5.1
1、第一步 安装 pymouse
pip install -i https://mirrors.aliyun.com/pypi/simple/ --upgrade pymouse
2、第二步:需要安装 PyUserinput、pyHook
若不安装PyUserinput,导入包pyMouse则报错:
ModuleNotFoundError: No module named ‘windows’
但安装 PyUserinput 之前需要先安装 pyHook,pyHook下载路径: 搜索 pyhook 下载
安装pyHook需要注意几点:
① pyHook 对应的版本最好与python版本相同,我的python版本为 3.7.6,因此下载 cp37
② pyHook 对于3.8及3.8以后的python版本不在维护
下载下来 .whl 文件后用 pip install 进行安装
安装方式:
pip install whl文件路径
3、第三步:安装 PyUserinput
pip install -i https://mirrors.aliyun.com/pypi/simple/ --upgrade PyUserinput
二、PyMouse、PyKeyboard 库简单实用
1、Pymouse简单使用
from pymouse import PyMouse
m = PyMouse()
a = m.position() #获取当前坐标的位置
print(a)
m.move(31, 223) #鼠标移动到(x,y)位置
a = m.position()
print(a)
m.click(31, 223) #移动并且在(x,y)位置左击
2、PyKeyboard简单使用
详情参考:PyKeyboard简单使用
自定义的小工具
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/11/21 16:44
# @Author :
# @Site :
# @File : test.py
# @Software: PyCharm
import time
import numpy as np
from pymouse import PyMouse
from pykeyboard import PyKeyboard
class Tools():
def __init__(self):
pass
@classmethod
def copy_(cls,keyboarder):
"""
PyKeyboard 复制操作
:param keyboarder: PyKeyboard 对象
:return:
"""
keyboarder.press_key(keyboarder.control_key)
time.sleep(0.1)
keyboarder.tap_key('c')
time.sleep(0.1)
keyboarder.release_key(keyboarder.control_key)
time.sleep(0.1)
@classmethod
def cut_(cls,keyboarder):
"""
PyKeyboard 剪切操作
:param keyboarder: PyKeyboard 对象
:return:
"""
keyboarder.press_key(keyboarder.control_key)
time.sleep(0.1)
keyboarder.tap_key('x')
time.sleep(0.1)
keyboarder.release_key(keyboarder.control_key)
time.sleep(0.1)
@classmethod
def parse_(cls,keyboarder):
"""
PyKeyboard 粘贴操作
:param keyboarder: PyKeyboard 对象
:return:
"""
keyboarder.press_key(keyboarder.control_key)
time.sleep(0.1)
keyboarder.tap_key('v')
time.sleep(0.1)
keyboarder.release_key(keyboarder.control_key)
time.sleep(0.1)
@classmethod
def delete_(cls,keyboarder):
"""
PyKeyboard 删除操作
:param keyboarder: PyKeyboard 对象
:return:
"""
keyboarder.tap_key(keyboarder.delete_key)
time.sleep(0.1)
@classmethod
def click_(cls,mouser,button,time_wait = 0.1):
"""
PyMouse 点击操作
:param mouser: PyMouse 对象
:param button: 点击的次数
:param time_wait: 点击扣的等待时间
:return:
"""
position = mouser.position()
mouser.click(int(position[0]),int(position[1]),button)
time.sleep(time_wait)
@classmethod
def move_(cls,mouser,position,num = 60,time_ = 0.3):
"""
鼠标移动的函数
:param mouser: PyMouse 对象
:param position: 鼠标从当下的位置移动到指定position
:param num: 中间需要刷新的移动次数
:param time_: 移动过去需要的总时间
:return:
"""
corr_position = mouser.position() # 获取鼠标当前的位置
move_position = np.floor(np.linspace(corr_position,position,num + 1,endpoint=True)).astype(np.int32)
for i in range(1,num + 1):
tmp1_position = move_position[i]
mouser.move(int(tmp1_position[0]),int(tmp1_position[1]))
time.sleep(time_ * 1.0 / num)
@classmethod
def all_select(cls,keyboarder):
"""
PyKeyboard 全选操作
:param keyboard: PyKeyboard 对象
:return:
"""
keyboarder.press_key(keyboarder.control_key)
time.sleep(0.1)
keyboarder.tap_key('a')
time.sleep(0.1)
keyboarder.release_key(keyboarder.control_key)
time.sleep(0.1)
@classmethod
def backspace(cls,keyboarder):
"""
PyKeyboard backspace 操作
:param keyboarder:
:return:
"""
keyboarder.tap_key(keyboarder.backspace_key)
time.sleep(0.1)
if __name__ == '__main__':
m = PyMouse()
k = PyKeyboard()
time.sleep(3)
position = m.position()
Tools.click_(m,button=1)
Tools.all_select(keyboarder=k)
三、三国杀防掉线小案例
from pymouse import PyMouse
import numpy as np
import time
def auto_click(position,blank=10):
"""
:param position: 鼠标移动并点击的位置,tuple(x,y)
:param blank:鼠标下次移动最少的时间间隔,int
:return:
"""
# 随机化两次鼠标移动的间隔时间
time_random = np.random.randint(blank,blank + 10)
time.sleep(time_random)
mouse = PyMouse()
x_ = position[0]
y_ = position[1]
mouse.click(x_, y_,button=1) # 移动并且在(x,y)位置左击1次
time.sleep(4) # 设置两次点击的间隔为4秒
mouse.click(x_, y_, button=1) # 移动并且在(x,y)位置左击1次
time.sleep(2)
return mouse
if __name__ == '__main__':
# (146,71) (199,71) (250,71) (303,71) (374,71) (1564,1020) (1714,1020) (1830,1020)
# 鼠标点击的坐标
position_list = [(146, 71), (199, 71), (250, 71), (303, 71),
(374, 71), (1564, 1020), (1714, 1020), (1830, 1020)]
# 两次鼠标移动最小的时间
time_blank = 3
print('三国杀自动点击脚本开始执行,你有30s的时间调整至三国杀的页面,本次点击的坐标如下:\n{}\n'.format(position_list))
for i in range(30,0,-1):
str_ = '脚本开始倒计时:第{}秒'.format(i)
print(str_)
time.sleep(1)
print('\n自动点击脚本开始>>>>>\n')
while True:
# 打乱随机操作
np.random.shuffle(position_list)
for i in position_list:
print('当前鼠标双击的位置x轴y轴:{}-{}'.format(i[0], i[1]))
mouse = auto_click(i,blank=time_blank)
# 点击显示桌面终端,查看信息
mouse.click(170,1065,button=1)