How to Make Hotkeys in 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:

๐Ÿ“บ How to setup pip:
๐Ÿ“บ Auto Py to Exe:

๐Ÿ 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()



    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:
    and it talks about that I can simply write " (" but when executing it it tells me that I am missing an argument.
    "TypeError: press () missing 1 required positional argument: 'button'"
    help ( 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



    def execute():
    dragRel(-250, -50,.15,button='left')
    hotkey('ctrl', 'c')
    hotkey('ctrl', 'a')
    hotkey('ctrl', 'c')

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

    def on_release(key):
    if any ([Key in COMBO for COMBO in COMBINATIONS]):

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

  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 *