Keyboard input w/o 'enter'

Oct 18, 2011 at 10:11pm
I've seen this question asked on many forums, but nobody seems to have a definite answer.

Question:
"How does the programmer get keyboard input without waiting for the 'enter' key to be pressed?"

This question is sometimes re-phrased to:
"How can I get key-presses in real-time?"

Can someone please answer either of these questions?
There's a LOT of people who would appreciate an answer.

The only c++ keyboard input information that seems readily available (in abundance) to the public is through use of cin or cin.get(). This is unfortunate because many programs that are created rely on real-time keyboard input. (from games to text editors)

On some forums people recommended the use of other libraries. I believe that the standard libraries should have some function(s) for real-time key input, because it's so important. That lead me to ask the questions here where knowledge about the standard libraries is so abundant, in the hopes that someone more knowledgeable than myself would know the answer.
Oct 18, 2011 at 11:12pm
The standard libraries were created in such a way that the programmer would never have to know anything about the OS their program is running on. Having functions to get real-time input (OS specific) would break that philosophy. If you want cross-platform real-time input (among other things), check out some 3rd party libraries like SFML (games, http://www.sfml-dev.org/ ) or QT (GUI's, http://qt.nokia.com/products/ ). There are many more libraries out there, Google is your friend.
Oct 18, 2011 at 11:48pm
lindsey laho wrote:

I believe that the standard libraries should have some function(s) for real-time key input, because it's so important.

Sorry to say no. C/C++ standard doesn't know anything about keyboard. keyboard or key press event is handled by OS libraries.
Oct 19, 2011 at 12:16am
I believe that the standard libraries should have some function(s) for real-time key input, because it's so important


For console programs, it really isn't important.

(from games to text editors)


You'll note that neither of these are console programs. Such programs do not use iostream for input.
Last edited on Oct 19, 2011 at 12:17am
Oct 19, 2011 at 12:33am
This requires another library, but here's how I do it anyways...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <conio.h>
//Acknowledgment: Thanks goes to the following webpages which provided the
//info needed to read the arrow keys from the keyboard (used in getKeyPress):
//	http://www.daniweb.com/software-development/cpp/code/216732
//	http://www.codingforums.com/archive/index.php/t-100973.html
char getKeyPress()
{
	char key = 127;

	key = _getch();
	
	if (key == 0 || key == -32)  //get a special key
	{
		key = _getch();

		if (key == 72) //up arrow
		{
			key = 'u';
		}
		else if (key == 75) //left arrow
		{
			key = 'l';
		}
		else if (key == 77) //right
		{
			key = 'r';
		}
		else if (key == 80) //down
		{
			key = 'd';
		}
	}
	return key;
}


EDIT: Not really sure which library you'll need... Might be #include <stdlib.h> or #include <windows.h> instead.
Last edited on Oct 19, 2011 at 3:37am
Oct 19, 2011 at 1:23am
Getting immediate input is not a language characteristic, but a terminal characteristic.
For console applications, you have several options.

NCurses/PDCurses
The venerable curses library is specifically designed for handling the terminal.
http://www.gnu.org/s/ncurses/
http://pdcurses.sourceforge.net/
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/
Example:
http://www.cplusplus.com/forum/lounge/2489/#msg9631

You can also use the native OS API to turn off line-buffering and echo.

For Windows, you need to use the SetConsoleMode() function. Here're a couple examples:
http://www.cplusplus.com/forum/beginner/3523/#msg15435
http://www.cplusplus.com/forum/beginner/3329/
http://www.cplusplus.com/forum/general/51988/

For *nix, you need to use the terminfo libraries. Again:
http://www.cplusplus.com/forum/general/5304/#msg23940
http://www.cplusplus.com/forum/beginner/5619/#msg25139
http://www.cplusplus.com/forum/beginner/3523/#msg15435

Good luck!
Topic archived. No new replies allowed.