Skip to content
Advertisement

pynput custom autokey script isn’t working when following video guide

I wanted to create a hotkey but decided to “challenge” myself by using Python instead of AHK.

I wanted the phrase Scheiße! printed out whenever I pressed the key combination Ctrl+Alt+P(or p)

I referred to this video and wrote the following script:

from pynput import keyboard

COMBINATIONS = [
{keyboard.Key.ctrl, keyboard.Key.alt, keyboard.KeyCode(char='p')}, # Detects ctrl+alt+p
{keyboard.Key.ctrl, keyboard.Key.alt, keyboard.KeyCode(char='P')} # Detects ctrl+alt+P
]

current = set()

def execute():
 print ("Scheiße!") # When the above combo is pressed, the word "Scheiße!" is printed

def on_press(key):
    if any([key in COMBO for COMBO in COMBINATIONS]): # Checks if pressed key is in any combinations
        current.add(key) # If it is, then it's added to the current key set
        if any(all (k in current for k in COMBO) for COMBO in COMBINATIONS): # Checks if every key of the combination has been pressed
                execute() # If all checks pass, execute is called
def on_release(key):
    if any([key in COMBO for COMBO in COMBINATIONS]): # Checks if a key released is in any of the combinations
        current.remove(key) # If it is, then it's remove from the current key set if applicable

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

When I execute the script, the following error(s) occurs:

    ================== RESTART: C:UsersIMSOASIANDesktopScheiße!.py =================
Unhandled exception in listener callback
Traceback (most recent call last):
  File "C:UsersIMSOASIANAppDataLocalProgramsPythonPython39libsite-packagespynput_util__init__.py", line 211, in inner
    return f(self, *args, **kwargs)
  File "C:UsersIMSOASIANAppDataLocalProgramsPythonPython39libsite-packagespynputkeyboard_win32.py", line 287, in _process
    self.on_release(key)
  File "C:UsersIMSOASIANAppDataLocalProgramsPythonPython39libsite-packagespynput_util__init__.py", line 127, in inner
    if f(*args) is False:
  File "C:UsersIMSOASIANDesktopScheiße!.py", line 20, in on_release
    current.remove(key) # If it is, then it's remove from the current key set if applicable
KeyError: 'p'
Traceback (most recent call last):
  File "C:UsersIMSOASIANDesktopScheiße!.py", line 23, in <module>
    listener.join()
  File "C:UsersIMSOASIANAppDataLocalProgramsPythonPython39libsite-packagespynput_util__init__.py", line 259, in join
    six.reraise(exc_type, exc_value, exc_traceback)
  File "C:UsersIMSOASIANAppDataLocalProgramsPythonPython39libsite-packagessix.py", line 702, in reraise
    raise value.with_traceback(tb)
  File "C:UsersIMSOASIANAppDataLocalProgramsPythonPython39libsite-packagespynput_util__init__.py", line 211, in inner
    return f(self, *args, **kwargs)
  File "C:UsersIMSOASIANAppDataLocalProgramsPythonPython39libsite-packagespynputkeyboard_win32.py", line 287, in _process
    self.on_release(key)
  File "C:UsersIMSOASIANAppDataLocalProgramsPythonPython39libsite-packagespynput_util__init__.py", line 127, in inner
    if f(*args) is False:
  File "C:UsersIMSOASIANDesktopScheiße!.py", line 20, in on_release
    current.remove(key) # If it is, then it's remove from the current key set if applicable
KeyError: 'p'

I’ve tried various troubleshooting steps such as removing the German characters, replacing ‘p’ and ‘alt’ with other keys, but the only thing that does is change the output.

Does anyone have a solution for this?

Advertisement

Answer

According to the information you provided, I tested the code in the video in VS Code, and it can be run in VS Code:

enter image description here

Please use the code:

COMBINATIONS = [
    {keyboard.Key.shift, keyboard.KeyCode(char='p')}, # Detects ctrl+alt+p
    {keyboard.Key.shift, keyboard.KeyCode(char='P')} # Detects ctrl+alt+P
]

In addition, please reload VS Code to make it recognize the installed modules, and try to run it several times in VS Code.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement