473,383 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 7182
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

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

Similar topics

8
by: Hung Huynh | last post by:
Hi, I'm trying to use either one of these methods to position the cursor in a specific position inside a recordset, but neither one seems to work. For example, I have 10 records in my rsData...
7
by: mcanedo | last post by:
Hi: I have several text elements in a form. Some of they are filled by the user, and then some are automatically filled based on the user input. Therefore I do not want the user to be able to...
7
by: Seash | last post by:
Hi friends , here is the sample code private void txtbox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if(e.KeyChar == 13) //enter { txtbox2.Focus(); }
1
by: gmtongar | last post by:
Hi, I've made a custom scrollbar which consists of three buttons and a panel. My problem is: How do I move it like a scrollbar slider? I've tried DoDragDrop, but it does not appear to be the same,...
1
by: Phil Endecott | last post by:
Dear Postgresql experts, According to the documentation for MOVE, it returns the number of rows that it has moved over. It seems to me that this is true for MOVE FORWARD n, but not for MOVE...
0
by: Rahna | last post by:
Hi, I'm using the DirectSS1 Speech Control in my VB 6 application. I tried to use the WordPosition Event to move the cursor along with the speech. the code goes like this Private Sub...
4
by: yan.python | last post by:
i have a question. when i run Interactive Interpreter in linux command promt,how can i move the cursor. for example,when i enter a string,i often enter the quotation mark "" first,and the move...
1
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
Can anyone offer pointers to articles/examples of passing a Ref Cursor ***IN*** to an Oracle stored procedure. I find tons of examples for getting a ref cursor OUT of a stored procedure. I'm using...
5
by: michels287 | last post by:
Right now I have a touchscreen with a virual keyboard. I would like to create arrow keys. If the user messed up the inputted text, he can move the blinking cursor left one space at a time between...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.