468,512 Members | 1,535 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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
Apr 18 '08 #1
2 6281
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
Apr 18 '08 #2
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:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <termios.h>
  4.  
  5.  
  6. int getch(void)
  7. {
  8. int ch;
  9. struct termios oldt;
  10. struct termios newt;
  11. tcgetattr(STDIN_FILENO, &oldt); /*store old settings */
  12. newt = oldt; /* copy old settings to new settings */
  13. newt.c_lflag &= ~(ICANON | ECHO); /* make one change to old settings in new settings */
  14. tcsetattr(STDIN_FILENO, TCSANOW, &newt); /*apply the new settings immediatly */
  15. ch = getchar(); /* standard getchar call */
  16. tcsetattr(STDIN_FILENO, TCSANOW, &oldt); /*reapply the old settings */
  17. return ch; /*return received char */
  18. }
  19.  
  20.  
  21. int main()
  22. {
  23. char x = ' ';
  24.  
  25. printf("Press any key to continue...\n");
  26. x = getch();
  27.  
  28. printf("The key you entered is: %c \n", x);
  29.  
  30. return 0;
  31. }
Apr 19 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by mcanedo | last post: by
1 post views Thread by Phil Endecott | last post: by
4 posts views Thread by yan.python | last post: by
1 post views Thread by =?Utf-8?B?cmFuZHkxMjAw?= | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.