Find the answer to your Linux question:
Results 1 to 3 of 3
How do you read keystrokes from the console? I'm making a console development toolkit and I want the user of this toolkit to be able to read keystrokes as numbers ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2005
    Posts
    3

    Reading Keystrokes from the Console


    How do you read keystrokes from the console? I'm making a console development toolkit and I want the user of this toolkit to be able to read keystrokes as numbers from the keyboard. I'm lost.

  2. #2
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    158
    You have to change terminal characteristics, read one keystroke, set the characteristics back to the original state:
    Code:
    #include <termios.h>
    #include <unistd.h>
    #include <assert.h>
    #include <string.h>
    /*------------------------------------------------*/
    int getch&#40;void&#41; &#123;
          int c=0;
    
          struct termios org_opts, new_opts;
          int res=0;
              //-----  store old settings -----------
          res=tcgetattr&#40;STDIN_FILENO, &org_opts&#41;;
          assert&#40;res==0&#41;;
              //---- set new terminal parms --------
          memcpy&#40;&new_opts, &org_opts, sizeof&#40;new_opts&#41;&#41;;
          new_opts.c_lflag &= ~&#40;ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL&#41;;
          tcsetattr&#40;STDIN_FILENO, TCSANOW, &new_opts&#41;;
          c=getchar&#40;&#41;;
              //------  restore old settings ---------
          res=tcsetattr&#40;STDIN_FILENO, TCSANOW, &org_opts&#41;;
          assert&#40;res==0&#41;;
          return&#40;c&#41;;
    &#125;

  3. #3
    Just Joined!
    Join Date
    Nov 2005
    Posts
    3
    Quote Originally Posted by jim mcnamara
    You have to change terminal characteristics, read one keystroke, set the characteristics back to the original state:
    Code:
    #include <termios.h>
    #include <unistd.h>
    #include <assert.h>
    #include <string.h>
    /*------------------------------------------------*/
    int getch&#40;void&#41; &#123;
          int c=0;
    
          struct termios org_opts, new_opts;
          int res=0;
              //-----  store old settings -----------
          res=tcgetattr&#40;STDIN_FILENO, &org_opts&#41;;
          assert&#40;res==0&#41;;
              //---- set new terminal parms --------
          memcpy&#40;&new_opts, &org_opts, sizeof&#40;new_opts&#41;&#41;;
          new_opts.c_lflag &= ~&#40;ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL&#41;;
          tcsetattr&#40;STDIN_FILENO, TCSANOW, &new_opts&#41;;
          c=getchar&#40;&#41;;
              //------  restore old settings ---------
          res=tcsetattr&#40;STDIN_FILENO, TCSANOW, &org_opts&#41;;
          assert&#40;res==0&#41;;
          return&#40;c&#41;;
    &#125;
    Thanks for the code. So this has to do with the terminal only. Will this take into account that I want the getchar() to return even when a key is not pressed?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •