python控制鼠标、键盘PyUserinput、pyHook、pyMouse、PyKeyboard的安装与简单使用

2024-01-03 15:39:00

一、安装

版本介绍:python 版本 3.7.6,pymousepymouse-1.0,pyHookpyHook-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简单使用(小工具)

参考用法

#!/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)

文章来源:https://blog.csdn.net/weixin_44697721/article/details/135362225
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。