import keyboard
# Say that your program will be moving and clicking the mouse and you won't
# be able to stop it. In the while loop that this is all happening, enter
# this:
while True:
# Stuff that moves mouse around
if keyboard.is_pressed("Esc"):
break