问题
I am trying to make a terminal snake game in C++ in macOSX. How do I detect which arrow key is pressed?
I tried to do this with getch() of ncurses.h but this is not solving the problem. No output is being detected.
#define KEY_UP 72
#define KEY_LEFT 75
#define KEY_RIGHT 77
#define KEY_DOWN 80
void Input()
{
int c = 0;
switch ((c = getch()))
{
case KEY_UP:
cout << endl<< "Up" << endl; //key up
break;
case KEY_DOWN:
cout << endl<< "Down" << endl; // key down
break;
case KEY_LEFT:
cout << endl<< "Left" << endl; // key left
break;
case KEY_RIGHT:
cout << endl<< "Right" << endl; // key right
break;
default:
break;
}
}
来源:https://stackoverflow.com/questions/57874850/how-to-detect-key-arrow-key-press-in-c-in-osx