I am a beginner at python programming, and I have been working on a pretty simple program to help me automate tasks in an app. Basically at the press of F5 I wish for a specific series of commands to execute, and at the press of f6 I wish for another specific of series of commands to run as well.
The way I have done it is using the pynput library: This code for detecting when I pressed f5 or f6:
with keyboard.GlobalHotKeys({
'<f5>': on_activate_f5,
'<f6>': on_activate_f6}) as h:
h.join()
Then the code to execute instructions (the function on_activate_f5 leads to this):
def leave():
printer("----LEAVE INITIATED----")
Controller().press(Key.esc)
Controller().release(Key.esc)
print("--step1 complete--")
pyautogui.click(300,420)
print("--step2 complete--")
pyautogui.click(920,560)
print("--step3 complete--")
printer("----LEAVE COMPLETE----")
However I am experiencing several issues with the code above:
1: First of all the "f" keys are not functionning correctly. When I replace the hotkeys code with this...
with keyboard.GlobalHotKeys({
'<ctrl>+<alt>+h': on_activate_f5,
'<ctrl>+<alt>+j': on_activate_f6}) as h:
h.join()
... then the keys ctrl alt and h / j are properly detected. However when I tried with all the f keys (f6, f5, f10 etc), none seem to be detected by pynput, although I got no error message :/
2: Whenever I try to press the escape key with the pynput keyboard it just leaves the app window to go back to the console. I have simply no clue why. It actually works perfectly with other keys (key.space) etc but EVERY time I use key.esc it just leaves the window.
The code in itself is actually much longer (around 300 lines) as it includes a basic tkinter GUI, threading etc.
If the problem resides in the rest of the code then I will ofc send the entire version, if anyone feels like it is necessary. Based on my testing however I am pretty confident the error is in these few lines.
Basically if anyone could explain to me how / why the "f" keys are not working with pynput and why using Controller().press(key.esc) leaves the window, I would very gratefull.
Complementary info:
Tank you for your time, feel free to ask anything !
You can find the answer by pressing "alt+esc" in any window. If you press the esc key in the program before you release the alt key from the hotkey, the window closes immediately because "alt+esc" is the hotkey that closes the window.