Pynput 控制器中的热键

Hotkey in Pynput Controller

如何使用 Pynput 控制器模拟热键按下,例如:shift + s + down 我希望同时按下 eg 中的所有三个键

我想要这样的东西:

from pynput.keyboard import Key, Controller

keyboard = Controller()

keyboard.press(Key.shift + 's' + Key.right)
time.sleep(0.1)            
keyboard.release(Key.shift + 's' + Key.right)

 

勾选Keyclasshere,得到所有可用键的列表。

你可以用函数模拟压力.press():

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

class MyClicker():
    def __init__(self, keys):
        self.keys = keys
        self.controller = Controller()

        self.start_press = False

    def Press(self, key):
        while not self.start_press:
            pass
        self.controller.press(key)

    def Run(self):
        #Create a thread for each key press
        for press_key in self.keys:
            threading.Thread(target=self.Press, args=(press_key,)).start()
        
        self.start_press = True
        time.sleep(0.1)
        self.start_press = False

        for release_key in reversed(self.keys):
            self.controller.release(release_key)

MyClicker([Key.shift, 's', Key.down]).Run()