Link to home
Start Free TrialLog in
Avatar of Jaerin
Jaerin

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

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.
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.
Avatar of Jaerin

ASKER

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

Thanks

Jaerin
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.)
Avatar of Jaerin

ASKER

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
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.
Avatar of Jaerin

ASKER

How about in an MS-Dos environment?

Thanks

Jaerin
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.