PeekConsoleInput

Recommended Posts

popcorn    151
I am trying to understand a bit more about windows console functions and came accross the PeekConsoleInput function which I think would be quite useful in games. Anyway I wrote a small program to test this function but it doesn't seem to work. Any help would be appreciated. The source is
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int main(int argc, char *argv[])
{
DWORD dwEventCount;
INPUT_RECORD irInRec;
char cHoldKey;
int cont = 1;

HANDLE hOut;
HANDLE hIn;

hIn = GetStdHandle(STD_INPUT_HANDLE);
hOut = GetStdHandle(STD_OUTPUT_HANDLE);

//GetNumberOfConsoleInputEvents(hIn, &dwEventCount);

while(cont)
{

if(irInRec.EventType == KEY_EVENT)
{
if(irInRec.Event.KeyEvent.bKeyDown)
{
cHoldKey = irInRec.Event.KeyEvent.uChar.AsciiChar;

if(cHoldKey == 'a')
{
COORD test2 = {0, 10};
SetConsoleCursorPosition(hOut, test2);
printf("a was pressed\n");
}

if(cHoldKey == 'd')
{
COORD test2 = {0, 10};
SetConsoleCursorPosition(hOut, test2);
printf("d was pressed\n");
}
}
FlushConsoleInputBuffer(hIn);
//GetNumberOfConsoleInputEvents(hIn, &dwEventCount);
}
}
system("PAUSE");
return 0;
}
[\source]



Share on other sites
Colin Jeanne    1114
What do you mean "doesn't work"? Does the function fail? If so, what does GetLastError() return?

Share on other sites
popcorn    151
Sorry I should've been more clear. What I mean is that when you compile and run the program nothing happens when you press a key. As you can see form the code when you press the letter 'a' it should print out a message saying "a was pressed" but it doesn't. Its not a problem with the function failing so I think it must be something else.

Share on other sites
Martee    476
Think carefully about what your code is doing here, and keep in mind that the input buffer may already have something in it by the time your code first reaches the PeekConsoleInput() statement. Suppose the input buffer initially contains a MOUSE_EVENT_RECORD. Nothing will ever be printed because FlushConsoleInputBuffer() will never be reached, and you will peek the same MOUSE_EVENT_RECORD every time. So there are a few ways to fix your code, the easiest being to add a call to FlushConsoleInputBuffer() right before the call to PeekConsoleInput(). You could also use ReadConsoleInput() to remove any unwanted input events from the buffer.

Share on other sites
Colin Jeanne    1114
The problem is that you are calling FlushConsoleInputBuffer() only if the event was KEY_EVENT. However, when the console is first created you are getting another event, probably a WINDOW_BUFFER_SIZE_EVENT although I didn't check. Since you only look at the first event and since the first event is always this WINDOW_BUFFER_SIZE_EVENT event you never manage to flush the input buffer and are never able to read a key event.

Moving FlushConsoleInputBuffer() outside of the if fixes the problem.

Share on other sites
popcorn    151
Thanks for the replies guys. Thinking about it I should have checked the last parameter of the peekconsoleinput function after it was called to see how many events were read. i.e did something like printf("number of events read %d\n", dwNumRead);.

I understand what is happening now though, I completely forgot about the mouse as an input device. Thanks again.

Share on other sites
popcorn    151
Oh and just for anyone that is interested the first event that occurs when the console starts up is a FOCUS_EVENT.