why do I get the same value of "up down right left" key-press from getch()

StackOverflow https://stackoverflow.com/questions/18534929

  •  26-06-2022
  •  | 
  •  

Вопрос

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?

Это было полезно?

Решение

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;
            }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top