Monitoring an input buffer

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

Join Date: Feb 2005
Posts: 78
Reputation: nanodano is an unknown quantity at this point 
Solved Threads: 2
nanodano's Avatar
nanodano nanodano is offline Offline
Junior Poster in Training

Monitoring an input buffer

 
0
  #1
Jun 1st, 2006
I've been trying to solve this problem for a while now with no luck. I want to monitor an input buffer(cin) for a keypress. The catch is, I don't want it to hang if the buffer is empty. getch() and peek() won't work because one waits for a keypress and the other waits for the return key. Is there any way to check for a keypress, but move on if there was no keypress? I know there is a DOS interrupt available in assembly, but I don't know how to do it with C.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,663
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 725
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Monitoring an input buffer

 
0
  #2
Jun 1st, 2006
If you have getch, you might have kbhit. That would be the easiest solution.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 78
Reputation: nanodano is an unknown quantity at this point 
Solved Threads: 2
nanodano's Avatar
nanodano nanodano is offline Offline
Junior Poster in Training

Re: Monitoring an input buffer

 
0
  #3
Jun 1st, 2006
Narue,

I looked up kbhit() and I read that it typically was a Borland Turbo C++ thing. Sure enough though, MS Visual Studio 2005 has a _kbhit(). It worked like a charm! The thing is, kbhit() does not return the key you pressed, only wether a key is pressed or not. So, I wrote a simple piece of code for anyone who might be interested in doing something similar. Also, I noticed that if you fill the buffer with more than one keystroke, and you use getch() it gets the keys one at a time in the order you pressed them.

  1. #include <iostream>
  2. #include <conio.h>
  3. #include <windows.h>
  4.  
  5. using namespace std;
  6.  
  7. void main() {
  8. int hit;
  9. char ch = 0;
  10.  
  11. do {
  12. hit = kbhit();
  13. if (hit)
  14. ch = getch();
  15. else
  16. ch = 0;
  17. cout << ch << endl;
  18. Sleep(100);
  19. } while(ch != 27);
  20. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 78
Reputation: nanodano is an unknown quantity at this point 
Solved Threads: 2
nanodano's Avatar
nanodano nanodano is offline Offline
Junior Poster in Training

Re: Monitoring an input buffer

 
0
  #4
Jun 1st, 2006
I'd like to add something (in case anyone is interested). An issue with the code I previously posted was that if there was a lot of time between calls to kbhit() and you only used one call to getch() then you might still have keystrokes left in the buffer. This might cause a problem if you want to assure you have the most recent keystroke. The simple solution is to nest a loop inside the if statement. I've included a ery slightly modified version of the previous code to show how to make sure you have only the most recent keypress.

  1. #include <iostream>
  2. #include <conio.h>
  3. #include <windows.h>
  4.  
  5. using namespace std;
  6.  
  7. void main() {
  8. int hit;
  9. char ch = 0;
  10.  
  11. do {
  12. hit = _kbhit();
  13. if (hit) {
  14. while (hit) {
  15. ch = _getch();
  16. hit = _kbhit();
  17. }
  18. }
  19. cout << ch << endl;
  20. Sleep(100);
  21. } while(ch != 27);
  22. }
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 1
Reputation: cniggeler is an unknown quantity at this point 
Solved Threads: 0
cniggeler cniggeler is offline Offline
Newbie Poster

Re: Monitoring an input buffer

 
0
  #5
May 2nd, 2009
Thanks to nanodano for his nifty piece of code! It's not all that easy to check the input buffer under Windows, and the sleep() feature does a nice job relinquishing the CPU while still checking periodically for user input...
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC