4

Who can help me on this?

Pause Countdown when press "P" Key and continue the Countdown when press "S" key. Until now i have this code, but i cant find the way to solve this.

Thanks

from multiprocessing import Process
import keyboard
import time


def countdown_1():
    i=6
    j=40
    k=0
    while True:
        a = keyboard.read_key()

        if(str(a) != "p"):    
            if(j==-1):
                j=59
                i -=1
            if(j > 9):  

                print(str(k)+str(i)+":"+str(j))
            else:

                print(str(k)+str(i)+":"+str(k)+str(j))

            time.sleep(1)
            j -= 1
            if(i==0 and j==-1):
                break
    if(i==0 and j==-1):
        print("END")
        time.sleep(1)


countdown_1()
Carlos Costa
  • 57
  • 1
  • 7
  • There is a similar solved issue [here](https://stackoverflow.com/questions/24072790/detect-key-press-in-python). Hope it helps – Daniel Saggo Feb 18 '20 at 13:14

1 Answers1

2

I get a solution to your problem, that is because when you use keyboard.readkey() python wait for a key to be pressed. instead, you should use keyboard.is_pressed('X')

I have modified your code to make a working version, I slightly change it to match my taste.

from multiprocessing import Process
import keyboard
import time


def countdown_1():
    pause_keyboard = False  # I use a bolean as a state is clearer for me
    i = 6  # minutes
    j = 40
    k = 0  # represent 0 we will instead use code format

    while True:

        starting_time = time.time()

        while True:  # this loop wait one second or slightly more

            if time.time() - starting_time >= 1: #  a second or more
                break

            if keyboard.is_pressed('p'):
                pause_keyboard = True

            elif keyboard.is_pressed('s'):
                pause_keyboard = False

        if pause_keyboard:
            continue


        if (j == -1): ## here we adjust the count when we changes minutes

            j = 59  # 59 secondes
            i -= 1  # one minutes less

        if(j > 9):  ## in order to pretty print

            print("{}{}:{}".format(0, i, j))  # you can direclty use 0 instead of k.
        else:
            print("{}{}:{}{}".format(0, i, 0, j))

        j -= 1

        if(i==0 and j==-1):  # we finish the counter
            break

    if(i==0 and j==-1):
        print("END")
        time.sleep(1) # wait a last second


countdown_1()

EDIT: Use time.time() instead of sleep to be able to catch signals.

RomainL.
  • 997
  • 1
  • 10
  • 24
  • Thanks... im running This on windows, but not stop when i press pause.. it works for you? – Carlos Costa Feb 18 '20 at 14:06
  • I tested it on ubuntu and it worked. sometimes the input is not recognized but I believe (without certainty) that is caused by sleep()... – RomainL. Feb 18 '20 at 14:35
  • Any other way? sorry :( – Carlos Costa Feb 18 '20 at 14:46
  • I am editing the code to use time instead of sleep. – RomainL. Feb 18 '20 at 14:46
  • dont run... Traceback (most recent call last): File "C:\Users\innov\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript exec(codeObject, __main__.__dict__) File "C:\Users\innov\Desktop\DISE-API\Weather\KEYBOARD.py", line 52, in countdown_1() File "C:\Users\innov\Desktop\DISE-API\Weather\KEYBOARD.py", line 18, in countdown_1 if time.time() - starting_time() >= 1: # a second or more TypeError: 'float' object is not callable – Carlos Costa Feb 18 '20 at 16:00
  • My bad, that a typo because starting_time is a variable it should be no parenthesis. – RomainL. Feb 18 '20 at 17:36