简单小练习_自定义快捷键
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)