Sign in to follow this  

PeekConsoleInput

This topic is 4821 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 dwNumRead;
  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)
  {
    PeekConsoleInput(hIn, &irInRec, 1, &dwNumRead);
           
    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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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.

*I hate you Martee [sad]

Share this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites

This topic is 4821 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this