943,712 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1880
  • C++ RSS
Jul 26th, 2009
0

Listening for keyboard input without pausing the program?

Expand Post »
How would one listen for keyboard input, without causing the program to pause waiting for the input? For instance say you have something like
C++ Syntax (Toggle Plain Text)
  1. float num = 0;
  2. bool runloop = true;
  3. for(int i=0; i<1000000 && runloop; i++{
  4. n += some_time_consuming_transformation(i);
  5. // How to listen for input at this point? e.g. waitforoptionalinput() function
  6. }
  7. printf("n is %d\n", n);
And you want it to run indefinitely unless the user enters some key, say "q". And if the user enters "q" it would call some other function, similar to
C++ Syntax (Toggle Plain Text)
  1. function verify(){
  2. printf("Are you sure you want to stop here? (y/n)\n");
  3. char y_or_n[1];
  4. cin >> y_or_n;
  5. if(strcmp("y",y_or_n)==0 || strcmp("Y",y_or_n)==0) runloop = false;
  6. }
Also, would there perhaps be a way to in advance tell the program to listen for keyboard input at any point - without explicitly calling a waitforoptionalinput() at a certain point in the program.
Reputation Points: 10
Solved Threads: 0
Light Poster
Kadence is offline Offline
46 posts
since Dec 2004
Jul 26th, 2009
0

Re: Listening for keyboard input without pausing the program?

You can use the GetAKeySyncState() function from windows.h.
Reputation Points: 38
Solved Threads: 13
Junior Poster
SeeTheLite is offline Offline
109 posts
since Mar 2009
Jul 27th, 2009
0

Re: Listening for keyboard input without pausing the program?

Thanks, but what about for Linux?

I also couldn't find any references or docs online for GetAKeySyncState(), and when I try to compile (after including windows.h) on my Windows machine I get
Quote ...
error: 'GetAKeySyncState' was not declared in this scope".
Reputation Points: 10
Solved Threads: 0
Light Poster
Kadence is offline Offline
46 posts
since Dec 2004
Jul 27th, 2009
0

Re: Listening for keyboard input without pausing the program?

Its been a while since I used it, but I'm pretty sure it grabs the current key being pressed down and returns its value(was for making a tetris game in class, I can't say I knew all that much back then). As for linux, you can disable the input buffer for cin(or something along the lines of this), which isn't really recommended. Alternatively you can run it on a seperate thread.
Reputation Points: 38
Solved Threads: 13
Junior Poster
SeeTheLite is offline Offline
109 posts
since Mar 2009
Jul 27th, 2009
0

Re: Listening for keyboard input without pausing the program?

Click to Expand / Collapse  Quote originally posted by SeeTheLite ...
Alternatively you can run it on a seperate thread.
That would be my first suggestion. You could also think about using pipe()/fork() and have one process listen for messages while the rest of your program executes.
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006
Jul 27th, 2009
0

Re: Listening for keyboard input without pausing the program?

Click to Expand / Collapse  Quote originally posted by Kadence ...
I also couldn't find any references or docs online for GetAKeySyncState(), and when I try to compile (after including windows.h) on my Windows machine I get
In Win32 it's GetAsyncKeyState() or GetKeyState().
Reputation Points: 76
Solved Threads: 40
Junior Poster
MrSpigot is offline Offline
156 posts
since Mar 2009
Nov 2nd, 2011
-1
Re: Listening for keyboard input without pausing the program?
GetAsyncKeyState(32) will return non zero if key 32(which happens to be spacebar) is pressed.
if spacebar is not being pressed then it returns 0. this runs in the background and the console does not have to be the active window to receive input. it also does not make the console wait for the key to be pressed. to make it wait for that key you could do:
do{}while(!GetAsyncKeyState(32))
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mtbfreak is offline Offline
1 posts
since Nov 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Class project could use some help
Next Thread in C++ Forum Timeline: Opening files C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC