[Python Study Notes]pynput实现对键盘控制与监控

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
>>文件: 键盘控制.py
>>作者: liu yang
>>邮箱: [email protected]
>>博客: www.cnblogs.com/liu66blog

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys, os
from pynput.keyboard import Controller,Key,Listener

# 监听按压
def on_press(key):
    try:
        print("正在按压:",format(key.char))
    except AttributeError:
        print("正在按压:",format(key))

# 监听释放
def on_release(key):
    print("已经释放:",format(key))

    if key==Key.esc:
        # 停止监听
        return False

# 开始监听
def start_listen():
    with Listener(on_press=on_press,on_release=on_release) as listener:
        listener.join()

if __name__ == '__main__':

    # 实例化键盘
    kb=Controller()

    # 使用键盘输入一个字母
    kb.press('a')
    kb.release('a')

    # 使用键盘输入字符串,注意当前键盘调成英文
    kb.type("hello world")

    # 使用Key.xxx输入
    kb.press(Key.space)

    # 开始监听,按esc退出监听
    start_listen()

转载于:https://www.cnblogs.com/liu66blog/p/8455532.html

你可能感兴趣的:([Python Study Notes]pynput实现对键盘控制与监控)