Skip to content
Advertisement

GlobalHotKeys pynput not working not reacting to function keys

community. I’m trying to put together a quick hotkey script in python here. For some reason it doesn’t react to function keys, meaning the expression '<ctrl>+<F2>': function_1 doesn’t work.

I was not able to find any clues in the official documentation or other examples online. Any thoughts?

Here is the script for testing.

from pynput import keyboard

def function_1():
    print('Function 1 activated')

def function_2():
    print('Function 2 activated')

with keyboard.GlobalHotKeys({
        '<ctrl>+<F2>': function_1,
        '<ctrl>+t': function_2}) as h:
    h.join()

Advertisement

Answer

I solved this issue by moving away from pynput Global Hotkeys and using just keyboard instead. I still don’t understand the nature of this issue with global hotkeys not recognizing F1 key..

Here is the solution I used. I also added the passthrough of values with lambda function for each hotkey.

import keyboard

def func1(key):
    print("F1 is pressed with value: ", key)

def func2(key):
    print("F2 is pressed with value: ", key)

# define hotkeys
keyboard.add_hotkey('F1', lambda: func1("value1"))
keyboard.add_hotkey('F2', lambda: func2("value2"))

# run the program until 'F12' is pressed
keyboard.wait('F12')
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement