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

Posted 刘六六

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Python Study Notes]实现对键盘控制与监控相关的知识,希望对你有一定的参考价值。

\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'
>>文件: 键盘控制.py
>>作者: liu yang
>>邮箱: liuyang0001@outlook.com
>>博客: 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()

以上是关于[Python Study Notes]实现对键盘控制与监控的主要内容,如果未能解决你的问题,请参考以下文章

[Python Study Notes]psutil模块

[Python Study Notes] psutil模块

[Python Study Notes]物体运动检测

[Python Study Notes]电池信息

[Python Study Notes]内存信息

[Python Study Notes]cpu信息