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?
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?
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.
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:
#include <iostream>
#include <conio.h>
usingnamespace 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();
returntrue; // Key Was Hit
}
returnfalse; // 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!
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.
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"
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.