ncurses problem

Nov 5, 2012 at 7:04pm
Hi,

I use LEFT-ArrowKey and RIGHT-Arrowkey to control a commandline program from "outside". The problem: If no key is used (default), I don't get the cursor back, because endwin() doesn't return the screen before the ncurses-initialization. After the programm has finished (default-case), any kind of key must be pressed to show the screen (before ncurse was initialized) again.

Any idea?

Regards
Thomas

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int ch;
initscr();
raw();
keypad(stdscr, TRUE);
noecho();

while (1)
{
  ch = getch();
  switch (ch)
  {
    case KEY_LEFT):
      //...
      break;
    case KEY_RIGHT:
      //...
      break;
    default:
      //...
      break;
  }
}
endwin();
//... 
Nov 5, 2012 at 7:14pm
¿How are you exiting the infinite loop?
Nov 5, 2012 at 7:16pm
The main with

 
return 0;
Nov 5, 2012 at 9:31pm
¿?
If you are simply doing default: return 0; then endwin() does not execute.
Nov 6, 2012 at 12:18am
This doesn't work. Ncurses is initialized...
Nov 6, 2012 at 1:08am
I know, the problem is that you are not terminating it.
Nov 6, 2012 at 7:34am
I tried getchar() and there is nearly the same problem: Instead of using one (any) key to finish the program in default-case, there must be used ENTER for one time...
Nov 6, 2012 at 4:51pm
ne is asking how you get to endwin() from your while(1) infinite loop.
Nov 6, 2012 at 6:25pm
After endwin() the main program ends (return 0). You can do endwin() into the key-cases. Here a short complete example. You can see, why it is difficult, if you do "exit(0)" out of the code 3rd key-case...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <stdio.h>
#include <string>
#include <ncurses.h>
#include <unistd.h>

#define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )

int main()
{
	int ch;
	initscr();			/* Start curses mode 		*/
	raw();				/* Line buffering disabled	*/
	keypad(stdscr, TRUE);		/* We get F1, F2 etc..		*/
	noecho();			/* Don't echo() while we do getch */

	while (1)
	{
		SLEEP( 100 );
		printw("Exit: Up arrow key\n");
		ch = getch();			/* If raw() hadn't been called
						 * we have to press enter before it
						 * gets to the program 		*/
		if(ch == KEY_SRIGHT)
		{
			printw("Shifted right arrow key\n");
		}
		else if(ch == KEY_SLEFT)
		{
			printw("Shifted left arrow key\n");
		}
		else if(ch == KEY_UP) // Up arrow key
		{
			endwin();			/* End curses mode		  */
			exit(0);
			//return 0;
		}
		else
		{	printw("The pressed key is ");
			attron(A_BOLD);
			printw("%c\n", ch);
			attroff(A_BOLD);
		}
		//refresh();			/* Print it on to the real screen */
		//getch();			/* Wait for user input */
	}

	endwin();			/* End curses mode		  */
	exit(1);
	//return 0;
}
Last edited on Nov 6, 2012 at 7:27pm
Nov 8, 2012 at 12:17pm
The missing link was

 
nodelay(stdscr,TRUE); //=non-blocking 


and

 
nodelay(stdscr,FALSE); 


Both could be used and changed - like necessary...
Last edited on Nov 8, 2012 at 12:58pm
Topic archived. No new replies allowed.