jD91mZM2
jD91mZM2

Reputation: 35

Capture Ctrl+C (SIGINT) when terminal is in raw mode?

So, imagine I have raw mode enabled in an application. The application may or may not take input.

What would be the best practise to capture Ctrl+C (SIGINT)? Remember it doesn't always take input, so that makes the whole thing a tiny bit harder.

My idea is that I always take input in the background, and use that to see if somebody presses Ctrl+C, and discarding all input unless, well, Ctrl+C, or that I do actually do something with the input.
Flaws with this idea is that I believe you can rebind SIGINT to other keys. Also, always taking input might not be the best practise, nor would it be completely painless to implement.

Do I just give up on raw mode completely? Do I only enable it when I need it? (A.K.A. every time I want a single char?)

Upvotes: 3

Views: 1595

Answers (1)

tbodt
tbodt

Reputation: 17007

Listen for character code 3, which gets sent when you press ctrl-c in raw mode.

Upvotes: 3

Related Questions