+-
使用C或C从控制台获取原始输入
/* Initialize new terminal i/o settings */
static struct termios old, new1;
void initTermios(int echo) {
    tcgetattr(0, &old); /* grab old terminal i/o settings */
    new1 = old; /* make new settings same as old settings */
    new1.c_lflag &= ~ICANON; /* disable buffered i/o */
    new1.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
    tcsetattr(0, TCSANOW, &new1); /* use these new terminal i/o settings now */
}

/* Restore old terminal i/o settings */
void resetTermios(void) {
    tcsetattr(0, TCSANOW, &old);
}

如何获得箭头键作为输入(可能作为单个字符),当前代码适用于我需要的所有其他东西…请不要使用ncurses解决方案

最佳答案
你不能在标准C/C++中这样做,你需要非标准的conio.h文件和getch()扩展名.然后你可以调用getch()两次来获取箭头的键代码

#include <conio.h>
using namespace std;

int main() 
{
    cout << "press up arrow;" << endl;
    int control = getch(); //gets the escape character
    int keycode = getch(); //gets the code
    cout << control << endl; 
    cout << keycode << endl;
}
点击查看更多相关文章

转载注明原文:使用C或C从控制台获取原始输入 - 乐贴网