0

I have a very simple Python3.8 script that is checking something in a while loop. I'd like to be able to press a key to stop that. The script was running on WSL2 Ubuntu 20.04, it's now running on a Ubuntu 20.04 AWS EC2 instance. I've tried tkinter & pynput, but they don't work for me on virtual Linux while using SSH. The scripts I've tried are straight copies from either the source sites or answers on Stackoverflow, so I've not included any of my code.

Is this possible? It seems a thing that should be to me.

Thanks for any help.

Deaton
  • 1

1 Answers1

0

Would using Ctrl+C count? You can detect this event as a KeyboardInterrupt in a try/except.

import time

try:
    for i in range(1000):
        print(f"Processing item {i}")
        time.sleep(1)
except KeyboardInterrupt:
    print(f"\nI got to {i} before I was interrupted")
scotty3785
  • 6,763
  • 1
  • 25
  • 35
  • Thanks. That's good to know. I'm going to delete this question, as I've got an answer now from here: https://stackoverflow.com/questions/292095/polling-the-keyboard-detect-a-keypress-in-python. The answer from "ullix" seems to do what I want :) I can't comment or up vote it as I'm using a new non-work account :( – Deaton Mar 11 '21 at 17:03