![]() ![]() ![]() This method is sort of already answered by user8167727 but I disagree with the code they made. You can stop all hooks by running this line: keyboard.unhook_all() Once executed, it will run the function when the key is pressed. I used _ because the keyboard function returns the keyboard event to that function. Keyboard.on_press_key("p", lambda _:print("You pressed p")) Using the function on_press_key: import keyboard It will wait for you to press p and continue the code as it is pressed. This is gonna break the loop as the key p is pressed. Using the function read_key(): import keyboard #Keyboard keys installYou can install this module using pip install keyboard More things can be done with keyboard module. While True: #Don't rely on this line of code too much and make sure to adapt this to your project. Markus von Broady highlighted a potential issue that is: This answer doesn't require you being in the current window to this script be activated, a solution to windows would be: from win32gui import GetWindowText, GetForegroundWindowĬurrent_window = (GetWindowText(GetForegroundWindow()))Äesired_window_name = "Stopwatch" #Whatever the name of your window should be The keyboard documentation is here for a more variated usage. The function above will print whichever key you are pressing plus start an action as you release the 'esc' key. For those who are on windows and were struggling to find an working answer here's mine: pynput from pynput.keyboard import Key, Listener ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |