私信  •  关注

Headcrab

Headcrab 最近创建的主题
Headcrab 最近回复了
6 年前
回复了 Headcrab 创建的主题 » 使用python创建输入文本的热键

这是一个很小的修改,做了你想要的,花了我大约2分钟的阅读时间 pynput 文档:

from pynput import keyboard
from pynput.keyboard import Key, Controller

kbd = Controller()

COMBINATIONS = [
        {keyboard.Key.shift, keyboard.KeyCode(char="p")},
        {keyboard.Key.shift, keyboard.KeyCode(char="P")}
        ]

current = set()

def execute():
    print("Dectected HotKey") #goes into the console window

    #"hitting" backspace to remove the "P",
    #must be unnecessary if we use some other modifier (Alt, Ctrl)
    kbd.press(Key.backspace)
    kbd.release(Key.backspace)

    kbd.type('Hi motherfucker!') #goes into the active app window

def on_press(key):
    if any ([key in COMBO for COMBO in COMBINATIONS]):
        current.add(key)
        if any(all(k in current for k in COMBO) for COMBO in COMBINATIONS):
            execute()

def on_release(key):
    if any([key in COMBO for COMBO in COMBINATIONS]):
        current.remove(key)

with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
    listener.join()

文本将是大写的,因为在“键入”的那一刻,你仍然要按住shift键。