Move cursor in cin input

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sandromani
    New Member
    • Dec 2007
    • 16

    Move cursor in cin input

    Hello,
    I was wondering if there was a way (in c++) to move the cursor when pressing the arrow keys instead of getting ^[[C resp. ^[[D (left resp. right arrow) in the cin input - I am using linux. I guess this behavior is controlled by the terminal, therefore not modifiable, or does anyone have an idea? I thought of getting one character at the time, check it against the arrow codes, and in case print the ANSI codes for moving the cursor, but the problem is that getting one character at the time also does not seem to be easy...
    Thanks for any inputs!
    Cheers,
    Sandro
  • mschenkelberg
    New Member
    • Jun 2007
    • 44

    #2
    Originally posted by sandromani
    Hello,
    I was wondering if there was a way (in c++) to move the cursor when pressing the arrow keys instead of getting ^[[C resp. ^[[D (left resp. right arrow) in the cin input - I am using linux. I guess this behavior is controlled by the terminal, therefore not modifiable, or does anyone have an idea? I thought of getting one character at the time, check it against the arrow codes, and in case print the ANSI codes for moving the cursor, but the problem is that getting one character at the time also does not seem to be easy...
    Thanks for any inputs!
    Cheers,
    Sandro

    Well, you have to do two things.

    1. disable terminal echo using the termio library. This involves creating a struct and turning the ECHO flag off. Sorry I don't have sample code, just look up "disable terminal echo"

    2. Once you have disable it, you can only read a char at a time in and you have to output it yourself if you want it to echo to the terminal. This allows you to move the cursor with the arrow keys.

    Max

    Comment

    • sandromani
      New Member
      • Dec 2007
      • 16

      #3
      Originally posted by mschenkelberg
      Well, you have to do two things.

      1. disable terminal echo using the termio library. This involves creating a struct and turning the ECHO flag off. Sorry I don't have sample code, just look up "disable terminal echo"

      2. Once you have disable it, you can only read a char at a time in and you have to output it yourself if you want it to echo to the terminal. This allows you to move the cursor with the arrow keys.

      Max
      Thanks for your answer, I googled a bit and found a code that does what I need based on the termio library. In case anyone else has the same problem, here is the code:
      Code:
      #include <stdio.h>
      #include <unistd.h>
      #include <termios.h>
      
      
      int getch(void)
      {
      int ch;
      struct termios oldt;
      struct termios newt;
      tcgetattr(STDIN_FILENO, &oldt); /*store old settings */
      newt = oldt; /* copy old settings to new settings */
      newt.c_lflag &= ~(ICANON | ECHO); /* make one change to old settings in new settings */
      tcsetattr(STDIN_FILENO, TCSANOW, &newt); /*apply the new settings immediatly */
      ch = getchar(); /* standard getchar call */
      tcsetattr(STDIN_FILENO, TCSANOW, &oldt); /*reapply the old settings */
      return ch; /*return received char */
      }
      
      
      int main()
      {
      char x = ' ';
      
      printf("Press any key to continue...\n");
      x = getch();
      
      printf("The key you entered is: %c \n", x);
      
      return 0;
      }

      Comment

      Working...