![]() |
|
#1
|
|||
|
|||
Getting arrow key's from a terminal immediatelyI was wondering whether it's possible to read keys input to a terminal immediately without having to engage curses, in a PC linux g++ coding environment. OR, set up curses so that it still allows printf and cout to display lines on the terminal.
I want to write a simple shell with a command history that can be scrolled using the arrow keys, but since I have a lot of legacy code using cout and printf to show various messages I'd rather not use the curses library because it supresses these output channels. I tried using getchar but that only returns when the user enters the whole line and I want to replace what the user input with the contents of the command history if they press an arrow key before they press return. Any suggestions? |
|||
|
#2
|
||||
|
||||
|
I believe you must use the function kbhit() for detecting arrow keys. It is located in the conio.h library.
CPP / C++ / C Code:
Bear in mind that conio.h is not a standard library and doesn't come with all compilers, however, Borland compilers usually do. I think MSVC 6.0 includes conio.h as well, but I am not certain. __________________
-Aaron |
|
#3
|
|||
|
|||
|
Quote:
You might check this out: http://www.gidforums.com/t-3386.html Regards, Dave |
|
#4
|
|||
|
|||
|
Thanks for your suggestions Dave and Aaron.
Unfortunately, I don't have conio.h, and the linux_getch code you posted blocks waiting for input. I need to read a character without blocking so I can suspend the thread the input handler is running in unless there is some keyboard input. I NEARLY managed to solve the problem! I've posted some code below that does non-blocking keyboard input and I tested for the key sequence substituted for the arrow keys, e.g., ^[[A as a string... probably not ideal but it seems to work. HOWEVER, there is one problem remaining, in that when I print a char 0x0D to go back to the start of the line, and replace the current input line with some new text (which is supposed to be taken from the command history but for now is just "UP" or "DOWN") the output doesn't appear until the getchar command is executed, and actually happens AFTER the echo to the terminal of the key pressed by the user... this doesn't happen if I don't use "select" to wait until input exists in stdin because in that case getchar gets called before the user gets a chance to input another character and the display is updated properly first. So the question now is: how can you flush the output buffer to the terminal immediately? cout.flush, fflush(stdout) etc. do not seem to have any effect! --------- Code example... sorry, I couldn't see how to put it in a tidy sub-window CPP / C++ / C Code:
Last edited by JdS : 01-Feb-2005 at 05:09.
Reason: Please insert your example C/C++ codes between [c] and [/c] tags
|
Recent GIDBlog
Managing sshd Brute-force Attacks with iptables by gidnetwork
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Receive arrow key input | tay | C++ Forum | 12 | 28-Aug-2008 17:54 |
Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The