码迷,mamicode.com
首页 > 其他好文 > 详细

简单小练习_自定义快捷键

时间:2018-06-26 13:50:13      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:run   except   code   type   turn   练习   bbb   name   TE   

from pynput.keyboard import Listener,Controller,Key
import time
import threading


class ComboListener:
    def __init__(self):
        self.cur_keys = []
        self.keymap = {
            bbb:wait a minute,
            aaa:good time
        }
        self.run()

    def _on_press(self,key):
        try:
            self.cur_keys.append(key.char)
        except AttributeError:
            self.cur_keys.append(key.name)

    def _cleaner(self):
        while True:
            time.sleep(0.7)
            self.cur_keys.clear()

    def _run(self):
        l = Listener(on_press = self._on_press)
        l.daemon = True
        l.start()

        t = threading.Thread(target=self._cleaner)
        t.daemon = True
        t.start()

    def get_combo(self):
        if len(self.cur_keys) >= 3:
            combo = self.cur_keys[-3:]
            return combo

    def get_parsed_combo(self):
        combo = self.get_combo()
        if combo:
            key = ‘‘.join(combo)
            if key in self.keymap.keys():
                return self.keymap[key]

def send(content):
    for _ in range(3):
        k.press(Key.backspace)
    k.type(content)

cl = ComboListener()
k = Controller()

while True:
    combo_content = cl.get_parsed_combo()
    if combo_content:
        send(combo_content)

 

简单小练习_自定义快捷键

标签:run   except   code   type   turn   练习   bbb   name   TE   

原文地址:https://www.cnblogs.com/Erick-L/p/9228648.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!