| | |
Monitoring an input buffer
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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. Narue,
I looked up
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. C++ Syntax (Toggle Plain Text)
#include <iostream> #include <conio.h> #include <windows.h> using namespace std; void main() { int hit; char ch = 0; do { hit = kbhit(); if (hit) ch = getch(); else ch = 0; cout << ch << endl; Sleep(100); } while(ch != 27); }
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. C++ Syntax (Toggle Plain Text)
#include <iostream> #include <conio.h> #include <windows.h> using namespace std; void main() { int hit; char ch = 0; do { hit = _kbhit(); if (hit) { while (hit) { ch = _getch(); hit = _kbhit(); } } cout << ch << endl; Sleep(100); } while(ch != 27); }
![]() |
Similar Threads
- Input Buffer (C)
Other Threads in the C++ Forum
- Previous Thread: Text and SDL
- Next Thread: Unresolved External !#?%&
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






