Detect CTRL-C

BlitzMax Forums/BlitzMax Beginners Area/Detect CTRL-C

Sub_Zero(Posted 2013) [#1]
Is it possible to detect CTRL-C being pressed from a commandline app?


Brucey(Posted 2013) [#2]
You can add a signal handler for SIGINT, on unixy platforms, and something like a SetConsoleCtrlHandler() on Windows.


Sub_Zero(Posted 2013) [#3]
How do i add a signal handler?


col(Posted 2013) [#4]
Hiya,

There are a couple of ways on Windows. I only know a Windows solution, maybe Brucey or someone else would help out with the other platforms?

This is a simple example that uses SetConsoleCtrlHandler to handle the CTRL-C :-


To test it you need to run the final generated exe. It won't work when the code is run from the IDE as the IDE thread picks up the CTRL-C and will terminate anyway. Use the close icon to close the console window.

Hope it helps!