Aug-26-2023, 06:21 PM
(This post was last modified: Aug-27-2023, 08:02 PM by Gribouillis.)
Hi everyone, I'm new in the forum, I don't have much programming knowledge, I'm just starting out. This is an Python 2.7 script and it asks a question when the 'esc' key is pressed to later receive a response, if 'y' is typed the script continues and therefore continues intercepting the mouse coordinates, when 'n' is typed the script stops and if a response different from the previous ones it indicates an error message and again allows me to enter an response. The problem is that I can't press the 'esc' key a fourth time, that is, suppose I capture the coordinates of the mouse for a minute and press 'esc', it asks the question, I answer with 'y' and again it continues capturing the coordinates, If this procedure is repeated two more times there is no problem, it is the fourth time, the script keeps capturing the mouse coordinates, it does not recognize that 'esc' is being pressed, therefore it does not launch the question nor stop, What is the cause of this error? And how can you modify the code so that you can press 'esc' on indefinite occasions?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#coding=utf-8 from pynput import keyboard from pynput import mouse running = True print ( 'The script is capturing the coordinates of the cursor...' ) def get_coords(x, y): if running: print ( "Now at: {}" . format ((x, y))) def on_press(key): global running if key = = keyboard.Key.esc: running = False print ( 'The script has stopped. Do you wish to continue? (y/n)' ) while True : answer = raw_input () if answer = = 'y' : running = True print ( 'The script was successfully resumed...' ) break elif answer = = 'n' : running = False print ( 'The script has finished...' ) return False else : print ( 'Invalid answer. Please enter "y" to continue or "n" to end the script.' ) with mouse.Listener(on_move = get_coords) as listen: with keyboard.Listener(on_press = on_press) as keyboard_listener: keyboard_listener.join() |
Gribouillis write Aug-27-2023, 08:02 PM:
Please don't remove content when the problem is solved. Read this help topic to learn about content removal in the forum.
Please don't remove content when the problem is solved. Read this help topic to learn about content removal in the forum.