For getch()
to work, you need to initialize ncurses at the start of your program. This is done by calling at least initscr()
and to disable line buffering, calling cbreak()
. From the cbreak man page:
Normally, the tty driver buffers typed characters until a newline or carriage return is typed. The cbreak routine disables line buffering and erase/kill character-processing (interrupt and flow control characters are unaffected), making characters typed by the user immediately available to the program. The nocbreak routine returns the terminal to normal (cooked) mode.
Initially the terminal may or may not be in cbreak mode, as the mode is inherited; therefore, a program should call cbreak or nocbreak explicitly. Most interactive programs using curses set the cbreak mode. Note that cbreak overrides raw.
Or instead or cbreak()
, another way to disable line buffering is by setting "raw mode" with raw()
.
For a complete example of reading keys in ncurses, see for instance
https://tldp.org/HOWTO/NCURSES-Programming-HOWTO/keys.html