What is the keycode of "Escape" while using curses.h????
is it KEY_EXIT?? I hv used this bellow but when i press escape, it prints "^[" instead of stopping the loop! and when i press "g", it does what i want the code to do when "escape" is pressed!!...Help! I am using "CodeBlocks 8.02" in Windows XP.

#include<stdio.h>
#include<curses.h>

int main()
{
    char c;
    int x=0,y=0;
    initscr();
    keypad(stdscr,TRUE);
    noecho();
    loop:
    c=getch();
    switch(c)
    {
        case KEY_RIGHT:
        {
            x++;
            move(y,x);
            goto loop;
        }
        case KEY_LEFT:
        {
            x--;
            move(y,x);
            goto loop;
        }
        case KEY_UP:
        {
            y--;
            move(y,x);
            goto loop;
        }
        case KEY_DOWN:
        {
            y++;
            move(y,x);
            goto loop;
        }
        case KEY_EXIT:
        {
            printf("Bye");
            break;
        }
        default:
        {
            printw("%c",c);
            goto loop;
        }
    }
    endwin();
    return 0;
}

Recommended Answers

All 2 Replies

hahaha!! I found out where was my mistake!! when I returned to delete my post, I could not find any option to delete or edit it, so replying to my own question, The code given below does exactly what I wanted it to do:

#include<stdio.h>
#include<curses.h>

int main()
{
    int c,x=0,y=0;
    initscr();
    keypad(stdscr,TRUE);
    noecho();
    c=getch();
    while(c!=27)
    {
        if(c==KEY_RIGHT)
        {
            x++;
            move(y,x);
        }
        else if(c==KEY_LEFT)
        {
            x--;
            move(y,x);
        }
        else if(c==KEY_UP)
        {
            y--;
            move(y,x);
        }
        else if(c==KEY_DOWN)
        {
            y++;
            move(y,x);
        }
        else
        {
            printw("%c",c);
            x++;
        }
        c=getch();
    }
    endwin();
    return 0;
}
commented: i'm going to give you a green, because I see you removed that ugly GOTO mess from your code. Just don't do it again. :) +6

When pressing Escape, it sends the escape character to the program. Unfortunately, curses ignores that when you enable KEYPAD. See, when you press the various function keys, arrow keys, etc, each of those codes start with the escape character.

So when curses sees an escape character, it assumes you pressed something that's not escape, and attempts to wait for the rest of the code to figure out what you "really" pressed.

One way to fix this is to disable blocking mode by using timeout(0) and nocbreak() to stop if from doing that sort of thing. The BEST way to fix this is to not use Esc when using keypad support. Use 'Q' or something of that nature.

By the way: don't use goto. Put your code in a loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.