Search code examples
pythonkeypresspynput

Hotkey in Pynput Controller


How to simulate a hotkey press using Pynput Controller for eg: shift + s + down I want all the three keys in the eg to be pressed simultaneously

I want something like this:

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)

 

Solution

  • Check the Key class here, to a list of all the avaiable keys.

    You can simulate pression with the function .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()