2

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?

Abhishek
  • 6,912
  • 14
  • 59
  • 85
Marco Lau
  • 579
  • 2
  • 10
  • 25
  • 1
    What's with the fancy quotes? – trojanfoe Sep 04 '13 at 05:39
  • possible duplicate of [What is equivalent to getch() & getche() in Linux?](http://stackoverflow.com/questions/7469139/what-is-equivalent-to-getch-getche-in-linux). Note that Linux does not have `conio.h` - this is Microsoft thing – mvp Sep 04 '13 at 05:45

1 Answers1

2

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 are getc() and getchar(), 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:

  • Control-A = 1
  • Control-Z = 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() or getc()). 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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • When you #include , are you using ncurses? Sorry, I am very confused with all the documentation available at the moment. – Marco Lau Sep 04 '13 at 06:17
  • If you're on Linux, yes, `#include ` would mean using ncurses (you'd need to add `-lncurses` to the command that links your program, too). Yes, there's room for confusion with all the documentation. – Jonathan Leffler Sep 04 '13 at 06:21
  • I doubt that ncurses is suitable to the OP's needs. Before using `getch()`, you need to do some initilaization, since it implicitly refers to the current window. Programs that use ncurses generally take over the entire screen. (Yes, I know this is a very old question; I saw it because somebody just edited it.) – Keith Thompson Feb 20 '15 at 23:39
  • @KeithThompson: I noted that there's initialization and finalization work to do with curses in the last sentence of the first paragraph. There are also options to use the `tc*()` functions and their relatives from ``. They also require initialization and finalization. – Jonathan Leffler Feb 20 '15 at 23:40