Search code examples
pythonpycharmtry-exceptkeyboardinterrupt

Why doesn't this Python keyboard interrupt work? (in PyCharm)


My Python try/except loop does not seem to trigger a keyboard interrupt when Ctrl + C is pressed while debugging my code in PyCharm. (The same issue occurs when using Ctrl + C while running the program, but not in the PyCharm Python console.)

My code look like this:

try:
    while loop:
        print("busy")

except KeyboardInterrupt:
    exit()

The full code can be viewed here. The code above produces the same error.


Solution

  • From your screen shot it appears that you are running this code in an IDE. The thing about IDEs is that they are not quite the same as running normally, especially when it comes to handling of keyboard characters. The way you press ctrl-c, your IDE thinks you want to copy text. The python program never sees the character. Pehaps it brings up a separate window when running? Then you would select that window before ctrl-c.