OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Checking for a Keypress to Pause a Program Midway through a loop in Python

  • Thread starter Thread starter Stephen Cook
  • Start date Start date
S

Stephen Cook

Guest
I am currently creating a web scraper to collect data for a project I am working on. However, the scraper I am using uses a Selenium Webdriver and Pyautogui, meaning that I can't really use my computer while it is active. What I would like to do is have a function to pause my program with a keypress (or something else simple) when I want to use my computer for other things, then resume my program with a simple input.

The problem I am encountering is that I want this pausing to happen at the beginning of every loop of my program. This loop takes a long time and checking for a keypress wasn't working how I was intending it to. This also does not have to be a keypress. I just want a way to stop my the code from running so that I can do other tasks during the day, then let the code run while I am not at my desk. Here is a similar example of what I am trying to achieve:

Code:
import time

paused = False

def observe_keypress_function():
    if key_was_pressed:
        print('Key has been pressed')
        paused = True

while True:
    if paused:
        input('Waiting for input')
    for i in range(1, 10):
        print(i)
        time.sleep(1)

this is the output I am looking for given a keypress during the loop

Code:
1
2
3
4  # Between 4 and 5 is a keypress
Key has been pressed
5
6
7
8
9
Waiting for input

Obviously, this doesn't need to be a function like this, but I want this to be what happens.

I tried using the keyboard library, but it didn't work for me. I also thought threads might be useful. That way I could detect the keypress at any time, then just wait until the loop completes to pause. However I have not played around with this enough to say if this would work or not (nor do I have that much experience with threads).

Thank you so much for the help!
<p>I am currently creating a web scraper to collect data for a project I am working on. However, the scraper I am using uses a Selenium Webdriver and Pyautogui, meaning that I can't really use my computer while it is active. What I would like to do is have a function to pause my program with a keypress (or something else simple) when I want to use my computer for other things, then resume my program with a simple input.</p>
<p>The problem I am encountering is that I want this pausing to happen at the beginning of every loop of my program. This loop takes a long time and checking for a keypress wasn't working how I was intending it to. This also does not have to be a keypress. I just want a way to stop my the code from running so that I can do other tasks during the day, then let the code run while I am not at my desk. Here is a similar example of what I am trying to achieve:</p>
<pre><code>import time

paused = False

def observe_keypress_function():
if key_was_pressed:
print('Key has been pressed')
paused = True

while True:
if paused:
input('Waiting for input')
for i in range(1, 10):
print(i)
time.sleep(1)
</code></pre>
<p>this is the output I am looking for given a <em>keypress</em> during the loop</p>
<pre><code>1
2
3
4 # Between 4 and 5 is a keypress
Key has been pressed
5
6
7
8
9
Waiting for input
</code></pre>
<p>Obviously, this doesn't need to be a function like this, but I want this to be what happens.</p>
<p>I tried using the keyboard library, but it didn't work for me. I also thought threads might be useful. That way I could detect the keypress at any time, then just wait until the loop completes to pause. However I have not played around with this enough to say if this would work or not (nor do I have that much experience with threads).</p>
<p>Thank you so much for the help!</p>
 

Latest posts

A
Replies
0
Views
1
Alfredo Augusto Petri
A
Top