Listening for keyboard input without pausing the program?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 42
Reputation: Kadence is an unknown quantity at this point 
Solved Threads: 0
Kadence Kadence is offline Offline
Light Poster

Listening for keyboard input without pausing the program?

 
0
  #1
Jul 26th, 2009
How would one listen for keyboard input, without causing the program to pause waiting for the input? For instance say you have something like
  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
  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 109
Reputation: SeeTheLite is an unknown quantity at this point 
Solved Threads: 12
SeeTheLite SeeTheLite is offline Offline
Junior Poster

Re: Listening for keyboard input without pausing the program?

 
0
  #2
Jul 26th, 2009
You can use the GetAKeySyncState() function from windows.h.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 42
Reputation: Kadence is an unknown quantity at this point 
Solved Threads: 0
Kadence Kadence is offline Offline
Light Poster

Re: Listening for keyboard input without pausing the program?

 
0
  #3
Jul 27th, 2009
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
error: 'GetAKeySyncState' was not declared in this scope".
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 109
Reputation: SeeTheLite is an unknown quantity at this point 
Solved Threads: 12
SeeTheLite SeeTheLite is offline Offline
Junior Poster

Re: Listening for keyboard input without pausing the program?

 
0
  #4
Jul 27th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 164
Reputation: Barefootsanders is an unknown quantity at this point 
Solved Threads: 3
Barefootsanders Barefootsanders is offline Offline
Junior Poster

Re: Listening for keyboard input without pausing the program?

 
0
  #5
Jul 27th, 2009
Originally Posted by SeeTheLite View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 143
Reputation: MrSpigot is on a distinguished road 
Solved Threads: 34
MrSpigot's Avatar
MrSpigot MrSpigot is offline Offline
Junior Poster

Re: Listening for keyboard input without pausing the program?

 
0
  #6
Jul 27th, 2009
Originally Posted by Kadence View Post
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().
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC