How to Make Hotkeys in Python

Python

Video is ready, Click Here to View ×


In this video I show you how to make a scrip that detects hotkeys and runs a method using Python. This works on windows, mac and linux. I use the pynput module to control the mouse.

๐ŸŒŽ Post with code and a written tutorial: http://nitratine.net/blog/post/how-to-make-hotkeys-in-python/

๐Ÿ“บ How to setup pip: https://youtu.be/cm6WDGAzDPM
๐Ÿ“บ Auto Py to Exe: https://youtu.be/OZSZHmWSOeM

๐Ÿ In this video I use Python 3.5.
๐Ÿ‘๐Ÿผ Feel free to comment with any issues.

19 thoughts on “How to Make Hotkeys in Python

  1. from pynput.keyboard import Key, Controller

    import time

    keyboard = Controller()

    time.sleep(2)

    keyboard.press(Key.crtl)

    keyboard.press(Key.esc)

    keyboard.release(Key.esc)

    keyboard.release(Key.crtl)
    why isnt it working? its says that crtl is an Attribute error

  2. Hi, I've been trying to make a bot for a game and I want to use the keyboard but without the need for the user to perform any action. how to press "a" or "A". I have consulted the pynput documentation: https://pythonhosted.org/pynput/keyboard.html
    and it talks about that I can simply write "keyboard.press (Key.space)" but when executing it it tells me that I am missing an argument.
    "TypeError: press () missing 1 required positional argument: 'button'"
    help (keyboard.press) tells me the same thing that two self and button parameters are required. I use py 3.7.2 latest version and updated, with its "path" variables correctly created and its pynput libraries installed immediately.

    ย I would appreciate any help ๐Ÿ˜€

  3. I have got 2 questions.
    1. what if i want to create different hotkeys to do different tasks?
    2. can I create new hotkey that also has a command like ctrl+v?

    great tutorial! thanks

  4. could you help me understand how to make this code so it doesnt just run one time. I want the program to be open so that I can repeatedly run this execute function. I decided to not learn python until i get a good grasp of javascript because Im already making some headway and dont want to be confused even more. which is why Im asking you to fix this one for me

  5. from pynput import keyboard
    from pyautogui import*
    import pyautogui

    COMBINATIONS = [
    {keyboard.KeyCode(char='z')}
    ]

    current=set()

    def execute():
    dragRel(-250, -50,.15,button='left')
    hotkey('ctrl', 'c')
    moveRel(150,0,.1)
    click()
    time.sleep(.5)
    hotkey('ctrl', 'a')
    hotkey('ctrl', 'c')
    press('backspace')

    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()

  6. Currently when activating the hotkey, you will write "A" as well since you're pressing the a button. Is there a way to disable it? Or should I just simulate a backspace key?

  7. for me it says AttributeError: module 'pynput.keyboard' has no attribute 'Keycode'
    iv had this with other modules but most of the time i can fix them by merely compiling them with a different shell
    but for this one that doesnt seem to be working
    (i know thats wrong terminology but whatever, so before someone claims "you cant run it with a different shell" or something, i know but i really dont care about trying to find the actual terminology)

  8. I found an important thing in your code. Currently, if you hold hotkey combination, it will send hotkey command over and over again. In my case it was turning mouse clicker on and off, with a 50/50 result at hotkey release. It easily fixable by adding additional 'and' condition in on_press function:
    if any([key in COMBO for COMBO in COMBINATIONS]) and not key in current

  9. This code works nicely, but I've run into two problems:
    -If I set the combinations to shift+A for example, whenever I press the combinations it works the first time, but after that it also works whenever I press A or shift by themselves (without pressing the other)
    -If I try to set the combination to Control+A, it doesn't work (maybe I'm doing it wrong, I'm using "keyboard.Key.ctrl" and "keyboard.Key.ctrl_l" and none of those work

    If you could help me,that'd be greatly appreciated

Leave a Reply

Your email address will not be published. Required fields are marked *