How to detect key arrow key press in C++ in OSX?

岁酱吖の 提交于 2021-02-10 20:30:56

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!