473,545 Members | 1,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Move cursor in cin input

16 New Member
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 7211
mschenkelberg
44 New Member
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
sandromani
16 New Member
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
15227
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 recordset, and I issue this command rsData.Move 5, 1 'move to #5 from beginning
7
11975
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 write on the autofilled text elements. I have come to the idea of give those elements the event onFocus=functiontoMovetoNextElement() Then my...
7
5761
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
8493
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, all though I can move the button. I have also used the mouseMove event, so I can move the button, but it's completely out of control. Still I feel it's...
1
1984
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 RELATIVE n or MOVE ABSOLUTE n when it always returns 1: db=> declare c scroll cursor for select * from p; DECLARE CURSOR db=> move absolute -1 in c;
0
1334
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 DirectSS1_WordPosition(ByVal hi as long, ByVal lo as long, ByVal ByteOffset as long) RichTextBox1.SelStart=ByteOffset/2
4
3292
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 the cursor inside the mark to enter the string,in windows,it is ok.but when i do that in linux,pressing the "left" key will just print "^[[D" in the...
1
7143
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 the Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory, NOT the ODT.NET library (please don't ask why). Thanks, Randy
5
8432
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 inputted characters to add a letter. Then a button to move the blinking cursor back to the end of the inputted text. Private Sub Left...
0
7459
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
5965
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5322
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4942
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3444
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3439
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1871
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
695
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.