Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to activate count down with hotkey while stop that count down with another hotkey? #620

Open
mywingdx0122 opened this issue Nov 18, 2023 · 0 comments

Comments

@mywingdx0122
Copy link

I want to register 2 hotkeys, such as "1" and "2". Press 1 to activate count down. While in counting down process, press 2 to interrupt the count down.

I'd like to know how to do this task?

Below is my code, but it does not work, press 2 will not stop the counting down.

Please help me, thanks in advance.

Code

COUNTING_DOWN_STOP_FLAG = False

def countDown():
    global COUNTING_DOWN_STOP_FLAG
    COUNTING_DOWN_STOP_FLAG = False
    for i in range(10):
        if COUNTING_DOWN_STOP_FLAG:
            print("Interrupted!")
            break
        sleep(1)
        print(10-i)

def stopDown():
    global COUNTING_DOWN_STOP_FLAG
    COUNTING_DOWN_STOP_FLAG = True


def try1():
    keyboard.add_hotkey("1", countDown)
    keyboard.add_hotkey("2", stopDown)
    keyboard.wait()

if __name__ == '__main__':

    try1()

Output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant