I have decided to use getch from conio.h on Linux. I have heard that this is not recommended but I need a solution now and work to improve my programming skills later.
I read a number of tutorials of how to enter one key and the program will do something. Such as:
printf("Press any key\n");
c = getch();
if (c)
printf(" A key is pressed from keyboard ");
else
printf("An error occurred ");
However if I want to use enter Ctrl+E to print 'A Ctrl was held with a key'. How would I go around doing this?
getch()
is a function found on Windows in#include <conio.h>
or on Unix in#include <curses.h>
. Did you mean to call one of those? It is not a function defined in the C standard (the standard functions aregetc()
andgetchar()
, of course). If you use the function from<curses.h>
, you need to do some initialization first and finalization afterwards.Assuming you resolve the issue of which function you are planning to call, then you would find that the control characters are number 1..26:
1
26
You might need to do some translation work on
getch()
from<curses.h>
— it returns interesting values for function keys and other special key strokes and might not return what you expect for the control keys.Also, your terminal driver may confuse you by interpreting various characters for you (especially if you use
getchar()
orgetc()
). For example, Control-D will likely be treated as EOF; Control-H is likely to be the backspace or erase; Control-C is likely to be the interrupt; and Control-Z is likely to be 'suspend' (meaning 'return to the shell without exiting the current program — just suspend it pro tem'). Other control keys have other meanings. You can often get the 'genuine' meaning by typing Control-VControl-Z, for example — using the Control-V to suppress the special meaning of the next character.