After searching around this forum for a bit, I found getch() in conio.h, this very closely matches the type of function I am looking for, but I don't want it to wait for the user to enter a key (It would be nice if it would just return -1 or something if the no key is pressed down.) , although my searches have failed to turn up something like this, I am pretty positive someone has made such a function, and creating threads to get past this problem is way past my current ability.

Thanks for your help. ( My first thread, but hopefully not my last on this forum :D )

Recommended Answers

All 12 Replies

What OS are you using?

ok so you want user to input and if theres no input for a while the input loop to close ???

I will search for it..... for now I have the idea to use getch in a loop, and set some timer..... I am googleing now for time.h .Will post results after some time.

I am running on windows, and what I want it to do is just check if a key is down, if one is, it returns the key code, if it isn't it returns perhaps -1, then the program can keep running without having to wait any amount of time for the user to press a key.

For example, in a dos game, perhaps a snake game, you don't want to wait for the user to press an arrow key to force the snake to move.

thats not possible if a user takes say 5 seconds betewwn inputs the program will give -1 just after the first key is pressed, you will nedd to wait fro a ceratin time like 1 s or 2s I am a begineer and I am googling for it.

You need to turn off line buffering using SetConsoleMode. Here is an example function that does that to wait for the user to press a key:

#include <string>
#include <windows.h>

void console::pressanykey( std::string prompt ) {
  DWORD        mode;
  HANDLE       hStdIn;
  INPUT_RECORD inrec;
  DWORD        count;
  char         default_prompt[] = "Press the 'any' key...";

  /* Set the console mode to no-echo, raw input, */
  /* and no window or mouse events.              */
  hStdIn = GetStdHandle( STD_INPUT_HANDLE );
  if (hStdIn == INVALID_HANDLE_VALUE
  or !GetConsoleMode( hStdIn, &mode )
  or !SetConsoleMode( hStdIn, 0 ))
    return;

  if (prompt.empty()) prompt = default_prompt; 

  /* Instruct the user */
  WriteConsole(
    GetStdHandle( STD_OUTPUT_HANDLE ),
    prompt.c_str(),
    prompt.length(),
    &count,
    NULL
    );

  FlushConsoleInputBuffer( hStdIn );

  /* Wait for and get a single key PRESS */
  do ReadConsoleInput( hStdIn, &inrec, 1, &count );
  while ((inrec.EventType != KEY_EVENT) || !inrec.Event.KeyEvent.bKeyDown);

  /* Restore the original console mode */
  SetConsoleMode( hStdIn, mode );
  }

Google "msdn SetConsoleMode" for more.

Hope this helps.

@ duoas which compiler u use ...... I don't have most of the header files u use, in my programs.

Thanks for your reply Duoas, but perhaps I should have mentioned I am a novice c++ programmer (who has never worked with Windows specific code before today). Which mode exactly am I shooting for, and what does it allow me to do?

I have searched for the text you gave me, found the site, and see that a lot of these modes are specific to ReadConsole, which I am not even using.

After testing a bit of that code, using
SetConsoleMode( hStdIn, 0 );
and
ReadConsoleInput( hStdIn, &inrec, 1, &count );

It does wait for the user to press a key, which I do not want, I just want to see if a key is pressed or not, and not wait for input.

Ack! I'm so sorry. I accidentally grabbed the wrong function... It's late and I've got to get some sleep but I'll post it early for you tomorrow.

atish00 The first is an STL header. The second comes with the Windows API.

You're looking for kbhit() , the fraterna; twin brother to getch()

Thanks a bunch WaltP, although it isn't exactly what I wanted, it was easy enough to use in conjunction with getch() to build exactly what I needed to get. :D (and even though you wasted my time I appreciate your effort duoas =p)

Thanks a bunch WaltP, although it isn't exactly what I wanted, ...

Yes it was. You already found the first half of what you needed, I supplied the rest of the puzzle. :icon_wink:

Fine, since you are a moderator, I will allow you to have known exactly what I wanted... but just this time! And if you think you know what I wanted exactly ever ever again, I will not want what you think I wanted therefore proving to the whole world with THOUGHT that you are a liarz.

Thanks again for the help though. I can now move throughout dos and type stuffs where-ever I want while avoiding falling smiley faces. (not what I wanted to build, but it was a nice test for this!)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.