synchronous keystroke reading from console application

前端 未结 3 1927
天命终不由人
天命终不由人 2021-01-29 03:01

I want to read every keystroke from a console application written in c under windows and linux immediately. Unfortunately the function gets(line) does only return a value, when

相关标签:
3条回答
  • 2021-01-29 03:43

    The following code worked for me. Thank you for pointing me into to right direction. http://bytes.com/topic/c/answers/503640-getch-linux

    #include <termios.h>
    #include <unistd.h>
    
    int mygetch(void)
    {
    struct termios oldt,
    newt;
    int ch;
    tcgetattr( STDIN_FILENO, &oldt );
    newt = oldt;
    newt.c_lflag &= ~( ICANON | ECHO );
    tcsetattr( STDIN_FILENO, TCSANOW, &newt );
    ch = getchar();
    tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
    return ch;
    
    }
    
    0 讨论(0)
  • 2021-01-29 03:49

    You're probably looking for getch(). On Windows (at least VC++) it's declared in <conio.h>. On Linux it's part of curses.

    0 讨论(0)
  • 2021-01-29 03:53

    I think you're looking for getchar() and putchar() :

    #include <stdio.h>
    char line[MAX_LINE];
    int i = 0;
    int c;
    while ( (c = putchar(getchar())) != EOF)
    {
        line[i] =c;
    }
    
    0 讨论(0)
提交回复
热议问题