Python Forum
[Python 2.7] Why can't I press [ESC] a fourth time?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Python 2.7] Why can't I press [ESC] a fourth time?
#1
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?

#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.
Reply
#2
What is raw_input()?
Reply
#3
(Aug-26-2023, 10:03 PM)deanhystad Wrote: What is raw_input()?

raw_input() is a function from Python 2, (which was renamed to input() in Python 3), and is used to request input from the keyboard in a console application. I'm coding in Python 2.7 so, I need to use raw_input. In this script when I press 'esc' it ask me if I want to continue or stop the script, If i send 'y' it will running again but if send 'n' it will exit. I can do this only three times, if I press 'esc' a fourth time, the script don't launch the question nor stop, it blocks on the coordinates capturing of the mouse and I don't know why this happens and how I could solve this.
Reply
#4
Works fine in 3.10, so the concept appears sound.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to change UTC time to local time in Python DataFrame? SamKnight 2 1,616 Jul-28-2022, 08:23 AM
Last Post: Pedroski55
Question convert autohotkey script to python / macro - press key when pixel get colour willson94d 1 3,669 Jan-01-2022, 08:13 PM
Last Post: Yoriz
  How I can multiply the fourth one from each row in the text file canatilaa 2 2,244 Dec-25-2018, 03:40 PM
Last Post: Larz60+
  How to run atom python script on idle press on keyboard shortcut. sonjoypall 2 5,151 Feb-07-2018, 07:51 AM
Last Post: buran
  Getting error in finding time.time() value in python Lakshana 1 2,505 Jan-11-2018, 07:07 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020