Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I read Ctrl+S in C?

Tags:

c

io

input

I have this program in C that reads the input like this:

cod1 = getch ();
  if (kbhit())
    cod2 = getch ();

I can read every Ctrl+Char possible sequences, except for Ctrl+C, that closes the program - that is OK, and Ctrl+S, that simple is not catch. But I wanted to make Ctrl+S to be the save function in my program; how could I do that? Furthermore, is it possible to read Alt+Char characters? Because it reads it as a regular character, e.g., Alt+A is read with the same codes as A.

like image 295
Luan Nico Avatar asked Nov 15 '12 10:11

Luan Nico


1 Answers

Your problem is that input probably gets eaten by terminal emulator.

For example Alt+<Whatever> is often reserved for menu shortcuts (e.g. Alt+F opens File menu). Matching characters are often hilighted once you hold Alt (F get's underscored in File).

Ctrl+S is reserved for Stops all output on screen (XOFF) (again your terminal emulator does that).

As for using Alt+<...> as shortcuts in your command line application. As far as I'm concerned holding Alt doesn't affect character received, it just sets flags which are hard to access in console. Even in GUI application (in Windows) it's quite tricky and you have to use function like GetAsyncState() to check whether alt was pressed.

like image 152
Vyktor Avatar answered Nov 15 '22 10:11

Vyktor