【Python】【pynput】键鼠控制



2018年12月29日    Author:Guofei

文章归类: 0xb0_Python语法    文章编号: 1251

版权声明:本文作者是郭飞。转载随意,但需要标明原文链接,并通知本人
原文链接:https://www.guofei.site/2018/12/29/pynput.html


基本用法

库地址 https://github.com/moses-palmer/pynput
使用前需要用pip命令安装

鼠标

import pynput
mouse = pynput.mouse.Controller()

# 鼠标位置,是一个形如(123,1022)的tuple,左上角是(0,0)
mouse.position # 位置,鼠标移动时,这个会刷新
mouse.position = (277, 645) # 也可以赋值,使鼠标移动到指定坐标
mouse.move(dx=5,dy=5) # 移动到相对距离

# 鼠标按键
mouse.press(pynput.mouse.Button.left)
mouse.release(pynput.mouse.Button.left)
# left/right/middle

# 多次点击
mouse.click(pynput.mouse.Button.left,count=2)

# 鼠标滑轮
mouse.scroll(dx=500,dy=-500) # dy为负是向下滑动,不知道为啥

监控鼠标事件

import pynput

mouse = pynput.mouse.Controller()


def on_move(x, y):
    '''
    不知道为啥,在 windows 上不起作用
    '''
    print('Pointer moved to {x},{y}'.format(x=x, y=y))


def on_click(x, y, button, pressed):
    '''
    :param x, y: 操作时的鼠标位置
    :param button: <enum 'Button'>对象,打印出来形如 BUtton.left, Button.right, Button.middle
    :param pressed: bool 对象,这次操作是按下还是松开。因此一次点击会调用两次本函数,一次拖拽也是。
    '''
    action = 'press' if pressed else 'release'
    print('{action} of {button} at {position}'.
          format(action=action, button=button, position=(x, y)))


def on_scroll(x, y, dx, dy):
    '''
    :param x,y: 操作时的鼠标位置
    :param dx, dy: 往下滑 dy 为负
    '''
    print('scrolled {s} at {p}'.format(s=(x, y), p=(dx, dy)))


with pynput.mouse.Listener(no_move=on_move, on_click=on_click, on_scroll=on_scroll) as listener:
    listener.join()

注:listener的用法还没搞清,官网示例是用一个while循环,我发现不用套循环也可以不停的监听(是不是跨操作系统需要用while循环?)
另外,官网还提供了一些方法:

listener = pynput.mouse.Listener(no_move=on_move, on_click=on_click, on_scroll=on_scroll)
listener.start() # 这样就开始工作了
listener.join() # 暂时不知道有什么用
listener.stop() # 停止监听

键盘

import pynput

keyboard.press(pynput.keyboard.Key.space)
keyboard.release(pynput.keyboard.Key.space)
# 似乎输入空格键无须 release
# alt, alt_l, alt_r, alt_gr, backspace, caps_lock, cmd, cmd_l, cmd_r, ctrl, ctrl_l, ctrl_r, delete, down, end, enter, esc, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, f15, f16, f17, f18, f19, f20, home, left, page_down, page_up, right, shift, shift_l, shift_r, space, tab, up, insert, menu, num_lock, pause, print_screen, scroll_lock

# 可以大写也可以小写
keyboard.press('A')
keyboard.release('A')

# 想要大写,也可以这样
keyboard.press(pynput.keyboard.Key.shift)
keyboard.press('a')
keyboard.release('a')
keyboard.release(pynput.keyboard.Key.shift)

# 直接输入一串字符也可以
keyboard.type('Hello world')

监听键盘事件

from pynput import keyboard


def on_press(key):
    try:
        print('alphanumeric key  {0} pressed'.format(key.char))
    except AttributeError:
        print('special key {0} pressed'.format(key))


def on_release(key):
    print('{0} released'.format(key))
    if key == keyboard.Key.esc:
        return False # 停止监听


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

参考文献

https://www.cnblogs.com/botoo/p/8302531.html
https://pythonhosted.org/pynput/mouse.html


您的支持将鼓励我继续创作!