How do I catch a Ctrl+C event in C++?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
For a Windows console app, you want to use SetConsoleCtrlHandler to handle CTRL+C and CTRL+BREAK.
See here for an example.
Yeah, this is a platform dependent question.
If you are writing a console program on POSIX, use the signal API (#include <signal.h>).
In a WIN32 GUI application you should handle the WM_KEYDOWN message.
signal
isn't the most reliable way as it differs in implementations. I would recommend usingsigaction
. Tom's code would now look like this :You have to catch the SIGINT signal (we are talking POSIX right?)
See @Gab Royer´s answer for sigaction.
Example: