How to check the keyboard without stopping?

Pages: 12
Is there a way in C++ to check if a key has been pressed without stopping the program?
E.g.:

1
2
3
4
5
6
7
while(true)
{
   ComputeSomething();

   if(keyPressed())
      break;
}


So I want the loop to keep running until I press a key. Does a (possibly standard) function such as "keyPressed()" exist?
Should I use exceptions? Is it possible to do it without exceptions?

Thank you.
Last edited on
A standard function for this doesn't exist.
On which OS do you need this?
Last edited on
I just need it for Linux, MacOS X and Windows...
From what you wrote I suppose I will have to use some #ifdefs.
Isn't there anything, e.g. in Boost or similar?
Last edited on
In Standard C++, no. All input functions are blocking. You have to fall back to system specific calls for non-blocking input checks.

ie, on Windows platforms, GetKeyboardState().
You could alternatively push ComputeSomething() to a separate thread.
> You could alternatively push ComputeSomething() to a separate thread.
Which is, again, not standard...

I will write a separate function whith a few #ifdefs inside, that will use what available on the different OSs.

Wouldn't it be useful to add such a function in the standard library?
I will write a separate function whith a few #ifdefs inside, that will use what available on the different OSs.
You can use cross-platform libraries

Wouldn't it be useful to add such a function in the standard library?
What you want is OS dependent so it isn't included in the standard library
Last edited on
> You can use cross-platform libraries

Could you name a few? (just if you already know about them; I do not want you to waste time...)

For multi-treading you have boost and others
There may be some also specifically for what you are looking for but I don't know them
Could you name a few? (just if you already know about them; I do not want you to waste time...)


It depends... what type of program are you writing?
Return 0: Basically I have some code that does some computation, and I want it to interact with a user, but without stopping waiting for him. For example, the user might want to stop the program (e.g. pressing the S key), and the program would exit the current loop, would save the results and it would exit.

I heard that such kind of non stopping interaction is not part of the standard library. I would like to know if there is some other commonly used library which implements these features in a plaform specific way for every OS, providing a common interface.
> You could alternatively push ComputeSomething() to a separate thread.
Which is, again, not standard...


Not standard, but there is no standard way to accomplish what you want. As Bazzy has mentioned though, there are cross-platform libraries available to help you avoid reinventing the wheel.

And if ComputeSomething() is computationally intensive, pushing it to a separate thread is also the ideal solution to keep user IO responsive as well, especially if it's likely to run on a multi-core platform.
I agree with jRaskell. It would be best to thread the application.

You could use PDCurses. I believe that will allow you to get input without stopping the program.

Oh and I don't know how Windows handles signals; but I think POSIX compliant or conformant systems will send signals around when keys are pressed...
Last edited on
It is 100% possible (and very easy) to check any keys pressed while looping in main program loop without stopping in console mode. And here is the code:

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
35
36
37
#include <iostream>
#include <conio.h>

using namespace std;

bool getInput(char *c);

// Program Loop
int main(void)
{
	char key = ' ';          
	
	while (key != 'q')     
	{
		while (!getInput(&key))
		{
			// Update your timer here
		}

		// Update Program Logic Here
		cout << "You Pressed: " << key << endl;
	}

	cout << "You Pressed q, End Of Program" << endl;
	return 0;
}

// Get Input
bool getInput(char *c)
{
    if (kbhit())
    {
         *c = getch();
         return true; // Key Was Hit
    }
	return false; // No keys were pressed
}


so this is full source code that uses old kbhit(); that is in
conio.h include file.

so here is the output you gonna have:

You Pressed: k
You Pressed: f
You Pressed: d
You Pressed: s
You Pressed: k
You Pressed: 4
You Pressed: 3
You Pressed: 4
You Pressed: d
You Pressed: s
You Pressed: f
You Pressed: d
You Pressed: l
You Pressed: Q
You Pressed: q
You Pressed Q, End Of Program
Press any key to continue


So program never stops (until you press 'q'), it runs and checks all keys you have pressed in real time!

Really hope it helps you!
Last edited on
conio.h is not standard, and isn't even cross platform most of the time. It could even vary between different versions of the same IDE's STL library.
The SDL provides cross-platform keyboard/mouse input and threading. And since it's open-source, you can take the source and inject it into your program to remove the need for the SDL DLL.
Last edited on
Hi,

trying using this function

http://msdn.microsoft.com/en-us/library/ms646293(VS.85).aspx

I think it reads the keyboard buffer, therefore your program won't stop when it bypass it.
ie when buffer is empty, input is nothing. but when the buffer is filled with a key, the function processes it.

hope this helps

ps though i cant find a way to do "press any key to end"
for windows, you can install keyboard hooks also.

http://msdn.microsoft.com/en-us/library/ms644990%28VS.85%29.aspx
I think the best approach would be to create a separate thread...
Doing so I can better separate the core code from the text mode/GUI one, and I will have to write a thin layer to use different threading libraries for different platforms.
...but I would guess that such a layer already exists somewhere... in Boost.


Last edited on
Boost already has a (cross platform) threading library
Last edited on
Pages: 12