Python之pynput监听和控制鼠标键盘

1、下载

python -m pip install pynput

2、监听鼠标事件

from pynput import mouse


def mouse_move(x, y):
    """
    鼠标移动事件
    :param x: 横坐标
    :param y: 纵坐标
    :return:
    """
    pass
    # print(f'鼠标移动X,Y:{x},{y}')


def mouse_click(x, y, button, pressed):
    """
    鼠标点击事件
    :param x: 横坐标
    :param y: 纵坐标
    :param button: 按钮枚举对象 Button.left 鼠标左键 Button.right 鼠标右键 Button.middle 鼠标中键
    :param pressed: 按下或者是释放,按下是True释放是False
    :return:
    """
    pass
    if pressed:
        print('按下鼠标')
        print(x)
        print(y)
        print(button)
    else:
        print('释放鼠标')
        print(x)
        print(y)
        print(button)
    print(''.center(20, '*'))
    if 500 <= x <= 550:
        print('返回False结束监听')
        return False


def mouse_scroll(x, y, dx, dy):
    """
    鼠标滚动事件
    :param x: 横坐标
    :param y: 纵坐标
    :param dx:滚轮的横坐标方向的移动量,0未移动,1向前面滚动和-1向后面滚动
    :param dy:滚轮的纵坐标方向的移动量,0未移动,1向前面滚动和-1向后面滚动
    :return:
    """
    pass
    print(x)
    print(y)
    print(dx)
    print(dy)


# 监听鼠标事件
with mouse.Listener(
        on_move=mouse_move,  # 鼠标移动事件
        on_click=mouse_click,  # 鼠标点击事件
        on_scroll=mouse_scroll  # 鼠标滚动事件
        ) as listener:
    listener.join()

3、监听键盘事件

from pynput.keyboard import Key
from pynput.keyboard import Listener


def keyboard_press(key):
    print(key)
    print(f'key:{key}')
    if Key.space:
        print('按下了空格')


def keyboard_release(key):
    print(f'key:{key}')
    if key == Key.esc:  # 如果按下了ESC键,则结束监听
        return False


with Listener(on_press=keyboard_press,
              on_release=keyboard_release) as listener:
    listener.join()

4、控制鼠标

from pynput.mouse import Button
from pynput.mouse import Controller

mouse = Controller()

position = mouse.position  # 获取当前的鼠标位置
print('当前的鼠标位置:{}'.format(position))

mouse.position = (1017, 236)  # 设置鼠标的位置,移动鼠标到该位置
print('移动鼠标到坐标点:{}'.format(mouse.position))

mouse.move(5, -5)  # 相对于当前鼠标位置位置移动鼠标

mouse.press(Button.left)  # 按下鼠标左键
mouse.release(Button.left)  # 释放鼠标左键
mouse.press(Button.right)  # 按下鼠标右键
mouse.release(Button.right)  # 释放鼠标右键
mouse.press(Button.middle)  # 按下鼠标中键
mouse.release(Button.middle)  # 释放鼠标中键

mouse.click(Button.left, 2)  # 双击鼠标左键

mouse.scroll(0, 2)  # 向下滚动滚轮两次

5、控制键盘

from pynput.keyboard import Key, Controller

keyboard = Controller()

# Press and release space
keyboard.press(Key.space)
keyboard.release(Key.space)

# Type a lower case A; this will work even if no key on the
# physical keyboard is labelled 'A'
keyboard.press('a')
keyboard.release('a')

# Type two upper case As
keyboard.press('A')
keyboard.release('A')
with keyboard.pressed(Key.shift):
    keyboard.press('a')
    keyboard.release('a')

# Type 'Hello World' using the shortcut type method
keyboard.type('Hello World')

6、监听键盘时可以这样看这个key有哪些属性

按下的键不同,key的属性还不一样!!!!!!!!!!!!

# -*- encoding=utf-8 -*-
from pynput.keyboard import Key
from pynput.keyboard import Listener
from pynput.mouse import Controller


def keyboard_press(key):
    print(f'按下的key:{key}')
    print(key.__dict__)
    if hasattr(key, 'char'):
        print(f'char:{key.char}')
    else:
        print('没有char属性')


def keyboard_release(key):
    print(f'key:{key}')
    if key == Key.esc:  # 如果按下了ESC键,则结束监听
        return False


if __name__ == '__main__':
    mouse = Controller()
    with Listener(on_press=keyboard_press,
                  on_release=keyboard_release) as listener:
        listener.join()

学习链接 https://blog.csdn.net/hskjshs/article/details/113521216?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.baidujs&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.baidujs

官网 https://pythonhosted.org/pynput/

posted @ 2021-06-11 17:31  南风丶轻语  阅读(2543)  评论(0编辑  收藏  举报