473,788 Members | 3,057 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 7250
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
15247
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
12014
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 question is how can I do that using the elements array?
7
5803
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
8502
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 the right way to go. This is what I've written: private void btnSecond_MouseMove(object sender,...
1
2009
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
1352
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
3316
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 screen ,but not what i want. so , how can i move the cursor Interactive Interpreter in linux? i've...
1
7164
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
8521
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 Arrow_Click() If Text1.SelStart Then Text1.SelStart = Text1.SelStart - 1 Text1.SetFocus End Sub Private...
0
9655
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.