1.

Solve : Keyboard input problem using Ncurses in C?

Answer»

Hey there. It's been a while since I've been around here.

I've run into a problem as I'm writing some simple code (mainly just to solve this problem) and I can't FIGURE out how to get around it.

Basically, when I want to take keyboard input, I could use keypad() but that causes a full 1000ms delay when the user presses the escape key.

This is because MANY keys ARE the escape key followed by other characters. (I'm talking non-ASCII, here; like arrow keys, function keys, etc.).

However, if I turn keypad() off, I can't figure out how to capture escape without having to hit it twice. All other keys work fine with the code I've written, it just hangs on Escape.

I understand WHY based on my code, I just don't know how to fix it.

Can anyone offer any solutions to the one second pause when using keypad() or a more successful way to manually capture raw keyboard input?

Here's my code:

Code: [Select]/*
* Author
* Venue
* Created: 00:00 Monday January 01, 2000
*
* Program description entered here
*
*/

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ncurses.h>

#define UP_ARROW 65
#define LEFT_ARROW 68
#define DOWN_ARROW 66
#define RIGHT_ARROW 67


/*
* Type Declarations
*/

/*
* Global Variables
*/

int cheat;

/*
* Prototypes & Structs
*/

void titles(WINDOW *one, WINDOW *two);
int main_menu(WINDOW *menu_win, int key_press, int selector);
int inchk(WINDOW *WIN);
int chtchk(int key_press);

/*
* int main(void):
*
* main() description
*
*/
int main(void)
{
int select, ch;
WINDOW *std_win;
WINDOW *extra_win;

initscr();
raw();
noecho();
cbreak();

if(has_colors() == FALSE)
{ endwin();
printf("Your terminal does not SUPPORT color\n");
exit(1);
}

start_color();
assume_default_colors(COLOR_WHITE, COLOR_BLACK);
init_pair(1, COLOR_BLACK, COLOR_CYAN);
init_pair(2, COLOR_BLACK, COLOR_YELLOW);

std_win = newwin(50,19,0,0);
extra_win = newwin(50,100,0,19);

wbkgd(std_win, COLOR_PAIR(1));
wbkgd(extra_win, COLOR_PAIR(2));

titles(std_win, extra_win);

wrefresh(std_win);
wrefresh(extra_win);

while (1 == 1)
{
do
{
select = main_menu(std_win, ch, select);
wrefresh(std_win);
ch = inchk(std_win);
if (ch == -1)
{
endwin();
exit(0);
}
}
while (ch != 10);
mvwprintw(extra_win, 5, 1, "You chose option %d. Press escape to quit or choose another.", select + 1);
wrefresh(extra_win);
}
endwin();

return 0;
}/* int main(void) */

int main_menu(WINDOW *menu_win, int key_press, int selector)
{
int selection, startx, starty, temp;


startx = COLS / 20;
starty = LINES / 10;

if (startx > 3)
{
startx = 3;
}
if (starty > 3)
{
starty = 3;
}

selection = selector;

if (key_press == UP_ARROW || key_press == LEFT_ARROW)
{
--selection;
if (selection < 0)
{
selection = 5;
}
}
else if (key_press == DOWN_ARROW || key_press == RIGHT_ARROW)
{
++selection;
if (selection > 5)
{
selection = 0;
}
}

mvwprintw(menu_win, 1, 1, "Main Menu");
mvwprintw(menu_win, 1 + starty, startx, " Option 1");
mvwprintw(menu_win, 2 + starty, startx, " Option 2");
mvwprintw(menu_win, 3 + starty, startx, " Option 3");
mvwprintw(menu_win, 4 + starty, startx, " Option 4");
mvwprintw(menu_win, 5 + starty, startx, " Option 5");
mvwprintw(menu_win, 6 + starty, startx, " Option 6");

wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, (selection + 1) + starty, startx, ">Option %d", selection + 1);
wattroff(menu_win, A_REVERSE);

return selection;
}

/*
* int titles(void):
*
* titles() description
*
*/
void titles(WINDOW *one, WINDOW *two)
{
mvwprintw(one, 1, 1, "Main Menu");
mvwprintw(two, 1, 1, "Game window?");
}/* int titles(void) */

/*
* int inchk(void):
*
* inchk() description
*
*/
int inchk(WINDOW *win)
{
char input;
input = wgetch(win);
if (input == '\033')
{
input = wgetch(win);
if (input == 27)
{
return -1;
}
}
else
{
return (int)input;
}

}/* int inchk(void) */
Any help would be appreciated. The INTERNET has NOT been beginner-friendly in my many C-related searches.

Also, I don't know if this is OS related or not, so I'm on a Unix operating system. I think it's Redhat. I'm running my software over an SSH connection to a university server, so I haven't delved very far into into it.

Thanks,
-rock



Discussion

No Comment Found