Question

I write a small program about B-Trix. And I want to use getch() to get gamer's input. I try to get the value of up,down,right,left key-press by using getch(), here is my test code:

#include <stdio.h>
#include <curses.h>
int main(void)
{
int ch;
initscr();
printw("Input a character:");
ch = getch();
printw("\nYou input a '%c'\n%d", ch, ch);
refresh();
sleep(3);
endwin();
return 0;
}

the outputs of up down left right are 27, why are these value same? Could anybody help me?

Was it helpful?

Solution

The arrow keys were encoded by three characters in Ubuntu. So I changed my code like this to check arrow keys.

if(kbhit()){
            switch(getch()){
            case 0x1b:          //For case arrow pressed
                if(getch() == 0x5b){
                    switch(getch()){
                    case 0x41:
                        turn();
                        break;
                    case 0x44:
                        mv_left();
                        break;
                    case 0x43:
                        mv_right();
                        break;
                    case 0x42:
                        mv_down();
                        break;
                    }
                }
                break;
            }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top