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.
JavaScript
x
13
13
1
from pynput import keyboard
2
3
def function_1():
4
print('Function 1 activated')
5
6
def function_2():
7
print('Function 2 activated')
8
9
with keyboard.GlobalHotKeys({
10
'<ctrl>+<F2>': function_1,
11
'<ctrl>+t': function_2}) as h:
12
h.join()
13
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.
JavaScript
1
15
15
1
import keyboard
2
3
def func1(key):
4
print("F1 is pressed with value: ", key)
5
6
def func2(key):
7
print("F2 is pressed with value: ", key)
8
9
# define hotkeys
10
keyboard.add_hotkey('F1', lambda: func1("value1"))
11
keyboard.add_hotkey('F2', lambda: func2("value2"))
12
13
# run the program until 'F12' is pressed
14
keyboard.wait('F12')
15