zoukankan      html  css  js  c++  java
  • [Python Study Notes]pynput实现对键盘控制与监控

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    >>文件: 键盘控制.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()
    
  • 相关阅读:
    优化算法-BFGS
    Go语言及Web框架Beego环境无脑搭建
    使用WCF扩展记录服务调用时间
    红黑树LLRB
    springmvc国际化 基于请求的国际化配置
    Adapter Pattern
    泡泡屏保
    使用WCF扩展在方法调用前初始化环境
    OAuth的一个.NET开源实现
    Google C++编程风格指南
  • 原文地址:https://www.cnblogs.com/liu66blog/p/8455532.html
Copyright © 2011-2022 走看看