468,242 Members | 1,721 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Reading keyboard input from a telnet session.

2
Hello all,

I am new to c++, coming from a Java background. Thank you in advance for anyone that can answer the question below:

GOAL:

I am currently writing a shell application for a telnet session. I wish to add functionality of a command buffer/history of past commands the user has
entered successfully. This action is similiar to pressing the up/down arrow on a standard prompt window and cycling through past executed commands.

PROBLEM:

I am having trouble listening to keystrokes. At first I tried to run select() on stdin with no timeout; however, select() blocks until the user presses the newline character on the prompt (press enter key).

Does anyone know of a way in the standard c++ library that you can read characters immediately after the user presses the key?

I wonder if the problem is that the telnet session is controlling the stdin and my program does not know of any input until the user presses the enter key, is this the case?
Sep 12 '07 #1
3 2858
weaknessforcats
9,207 Expert Mod 8TB
Your C++ Standard Library features require that you press the enter key before the program takes control. The user can enter any keys into the inout buffer but your program won't take charge until enter is pressed.

Your solution is to not use the C++ Standard Library.

With Windows you can process a WM_KEYDOWN event immediately.
Sep 12 '07 #2
Moogoo
2
Ok so the select() is a blocking function, unless you specify a timeout value?

So if I wanted to listen to keystrokes I would have to create my own thread to just read characters off the input stream?

e.g.
Expand|Select|Wrap|Line Numbers
  1. int i; char ch;
  2. while(true){
  3.  
  4.     i = read(fileno(stdin),&ch,1);
  5.  
  6.     if(i == 0)
  7.        continue;
  8.     else{
  9.        if(ch == "UpArrow") // don't know hexcode yet...
  10.            // process my command
  11.        else
  12.           // pass to shell
  13.     }
  14. }
Sep 12 '07 #3
weaknessforcats
9,207 Expert Mod 8TB
You can't use read to listen for keystrokes. The buffer is not available until enter is pressed.

You have to call functions that can listen to keystrokes. In a Windows program you process the WM_KEYDOWN message. These messages are wired to the keyboard itself and do noit use stdin and do not read a buffer.

I don't know what the Unix equivalent is.
Sep 12 '07 #4

Post your reply

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

Similar topics

3 posts views Thread by Yannick Turgeon | last post: by
4 posts views Thread by Donnal Walter | last post: by
2 posts views Thread by john brown | last post: by
5 posts views Thread by richardtinkler | last post: by
40 posts views Thread by googler | last post: by
2 posts views Thread by vmalhotra | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.