Keyboard Handling in C++

I am just beginning to learn C++ and I am wondering how you would go about handling keyboard input on the fly.  So the user would not have to press return to enter data or to navigate through menus.

Thanks

Jaerin
JaerinAsked:
Who is Participating?
 
nietodConnect With a Mentor Commented:
Standard C++ does not allow this.  IO have a stock answer on this.  I'll find it.
0
 
nietodCommented:
I guess I don't have a stock answer on this.  Well, I will now.

In standard C++, input is line buffered and blocking.  That means that when a an attempt to read input is started, the input will not be read until an entire line has been entered (this is line buffering).  (This was done because early computer terminals buffered lines entered by the user so the user would have an opportunity to correct the line (backspace and retype) before the line was sent to the computer,  It was not practical on early mainfames to allow the computer itself to dot he correction (handle backspace), so it was done by the terminal.)  Furthermore the attemnpt to read the input is blocking, that means that  when input is not yet available, that is when the line has not yet ended, the attempt to read will wait until the input is available.
0
 
nietodCommented:
Now that is standard C/C++ and if you restrict yourself to standard C/C++,t here is nothing you can do about it.  Fortunately, there are things that can be done about it.

First of all you can use non-standard C/C++ functions.  Many compiler manufacturers provide non-standard functions that don't perform line buffering and/or are not blocking.

Another option is to use operating system-specific functions to perform the I/O.  Many OSs will provide non-buffered, non-blocking I/O procedures.

Either of these approaches will work, but will make your source code unportable.  That is, it won't necessarily compule under another manufacturers compiler or on a different platform.

I amy be able to discuss some of these compiler-specific or OS-specific functions, if you want, and if you notify me of the compiler and OS you are using.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
JaerinAuthor Commented:
I am currently learning on a Slackware 7 Linux machine.  Using g++ as my compiler.  I access the machine using SecureCRT telnet.

Thanks

Jaerin
0
 
nietodCommented:
There is an add-on library for unix C compilers called "curses"  Do you have it?  It provides non-blocking I/O as well as cursor control (hence the name CURSes) (Cursor control, meaning the ability to move the cursor, control its size, and to control various aspects of the display, like color, font size etc.)
0
 
JaerinAuthor Commented:
Yes, the machine I have been working on has the curses library set.  Where could I find information on how to use this library to do what I am looking for.

Thanks

Jaerin
0
 
nietodCommented:
I haven't worked in Unix in 10+ years....

There is (was) help in the online help manual thing.  Was it like "man curses"  Something like that?  You may also be able to ask on one of the unix/linux programming topic areas.  This is not a rare/remote library.  Many unix programmers will be familiar with it.
0
 
JaerinAuthor Commented:
How about in an MS-Dos environment?

Thanks

Jaerin
0
 
nietodCommented:
Do you really mean DOS, or do you mean a windows console (A 32 bit DOS-like program)?

DOS provided a bunch of "services" for doing this sort of thing.  They can be called from C/C++, but it is a pain.  Most DOS compilers provided  non-standard procedures that acted as an interface to these services.  (you might find things like getch() or _getch(), keypressed(), _keypressed()).

Windows provides a bunch of very nice procedures for performing I/O from a console.  For example, ReadConsole().   However ReadConsoel() does do all this buffering etc by default but it can be controlled with GetconsoleMode().  A seach of these functions in the VC on-line help will lead to a bunch of other functions for dealing with the console--controlling screen atttributes, scrolling text, moving the cursor, settting the window title, etc.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.