Detecting keystrokes

前端 未结 3 870
清歌不尽
清歌不尽 2020-12-17 04:05

I need to detect a keystroke, without the user pressing enter. What\'s the most elegant way?

I.e. If the user hits the letter Q, without pressing ent

3条回答
  •  粉色の甜心
    2020-12-17 04:58

    In unix/posix, the standard way of doing this is to put the input into non-canonical mode with tcsetattr:

    #include 
    #include 
        :
    struct termios attr;
    tcgetattr(0, &attr);
    attr.c_lflag &= ~ICANON;
    tcsetattr(0, TCSANOW, &attr);
    

    See the termios(3) man page for more details (and probably more information than you wanted to know).

提交回复
热议问题