Getch does not work without initscr

I went through the NCURSES Documentation . I do not understand that if I use getch without initscr, then why this program does not work. Is there any other approach to entering arrow keys without a cleaning screen (which does initscr).

#include <ncurses.h> #include <unistd.h> int main() { int ch; //initscr(); //raw(); //keypad(stdscr, TRUE); //noecho(); while(1) { ch = getch(); switch(ch) { case KEY_UP: printw("\nUp Arrow"); break; case KEY_DOWN: printw("\nDown Arrow"); break; case KEY_LEFT: printw("\nLeft Arrow"); break; case KEY_RIGHT: printw("\nRight Arrow"); break; } if(ch == KEY_UP) break; } //endwin(); } 
+4
source share
3 answers

Alternatively, you can use the terminal attribute through tcsetattr in termios . If you switch between canonical mode (a new line is required to start the process) and non-canonocal mode (Keypress is more than enough).

The following program works as follows: the process is waiting for user input. If the up arrow key is pressed, it prints the "arrow arrow" and exits. If something else is pressed, it waits for the user to press Enter , and then prints the custom inpu. The outputs after entering inut are printed.

 #include <termios.h> #include <unistd.h> #include <fcntl.h> #include <stdio.h> int main() { struct termios oldt, newt; char ch, command[20]; int oldf; tcgetattr(STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &newt); oldf = fcntl(STDIN_FILENO, F_GETFL, 0); fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK); while(1) { ch = getchar(); if (ch == '\033') { printf("Arrow key\n"); ch=-1; break;} else if(ch == -1) // by default the function returns -1, as it is non blocking { continue; } else { break; } } tcsetattr(STDIN_FILENO, TCSANOW, &oldt); fcntl(STDIN_FILENO, F_SETFL, oldf); if(ch != EOF) { ungetc(ch,stdin);ith putchar(ch); scanf("%s",command); printf("\n%s\n",command); return 1; } return 0; } 
+1
source

getch same as wgetch(stdscr) . It is assumed that the screen has been initialized. ncurses (any curses implementation) needs to do a couple of things to make wgetch applicable:

  • read terminal description
  • initialize terminal input / output modes
  • highlight the screen to draw.

The latter is due to the fact that wgetch makes wrefresh in the window for which it was called before reading.

You can use newterm with filter to avoid clearing the screen and doing linear input. The filter program in ncurses-examples demonstrates how to do this.

+2
source

I have a solution without ncurses

You can use simple-getch as follows:

 t_key keys[] = { {"[A", K_UP}, {"[B", K_DOWN}, {"[D", K_LEFT}, {"[C", K_RIGHT}, {NULL, K_UNK}, }; int key; ch_init(); while ((key = ch_get(keys)) != K_BACK) { printf("%d\n", key); } ch_end(); 

keys array is a list of escape sequences that will be used (when you type a key with an arrow in the terminal, it will write an escape key followed by short characters to determine the key.)

These sequences can / will vary between terminals, you must use termcap to set these sequences correctly.

0
source

All Articles