Fix Python – How to kill a while loop with a keystroke?

I am reading serial data and writing to a csv file using a while loop. I want the user to be able to kill the while loop once they feel they have collected enough data.
while True:
#do a bunch of serial stuff

#if the user presses the ‘esc’ or ‘return’ key:
break

I have done something like this using opencv, but it doesn’t seem to….