0

I want to end a while loop with a keypad stroke but for some reason the loop completely skips over the msvcrt part of the loop that would do that. Is there possibly a better way to do this without having to download any libraries?

Code:

import datetime
import time
import msvcrt

# import cv2

# from datetime import date, time

while True:

    # if msvcrt.kbhit() and msvcrt.getch() == chr(27).encode():
    #   break

    name = input('\n' + "What is your name?")

    input('\n' + "Press any key to start the stopwatch")
    startTime = datetime.datetime.now()
    input('\n' + "Press any key to stop the stopwatch")
    endTime = datetime.datetime.now()

    time_out = ('\n' + "Time Out: " + str(endTime-startTime))
    print(time_out)

    student_out = ("Student Out: " + str(name))
    print(student_out)

    time_released = ("Time Released: " + str(startTime))
    print(time_released)

    time_back = ("Time Back: " + str(endTime))
    print(time_back)

    if msvcrt.kbhit() and msvcrt.getch() == chr(27).encode():
        break
  • Possible duplicate of [How to detect ESCape keypress in Python?](https://stackoverflow.com/questions/5137238/how-to-detect-escape-keypress-in-python) – eyllanesc Apr 01 '18 at 04:36
  • It's the same idea, but I don't know where I went wrong here; that part of the code doesn't work when it runs, and I suspect it has something to do with the while loop. – user8095302 Apr 02 '18 at 00:23
  • Never mind, I finally got it. Code: 'key = ord(getch()) if key == 27: #ESC break' – user8095302 Apr 02 '18 at 01:18

0 Answers0