If I understand you, you could use the module signal
from the standard library that allows us to Capture certain events asynchronously. Although there are some details of this module that are not cross-platform, in this case you would not have problems.
If it's important to keep in mind that it can only be used in the main thread if you use a multi-threaded application.
This would be a simple example:
import signal
import sys
import time
# Función que se ejecutará cuando el evento tenga lugar.
def keyboard_interrupt(signal, frame):
print('Has precionado Ctrl + c')
sys.exit(0) # Si quieres detener la ejecución
signal.signal(signal.SIGINT, keyboard_interrupt)
def run():
while True:
time.sleep(1)
if __name__ == "__main__":
run()