getch() with timeout

Posted on 2003-03-04
Medium Priority
Last Modified: 2007-12-19
Does anybody know how to get a character, just like getch(); however with a timeout implemented so that it won't wait forever.

This is going to be used in a console game (PONG) that asks for input to move the paddle, but if the user does not input a movement, then the getch() will timeout, and the ball will move...

Question by:birdgenj
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

Accepted Solution

jkr earned 150 total points
ID: 8065951
'_kbhit()' should be the solution for your problem, as there is no way to specify timeouts:

#include <conio.h>
#include <stdio.h>

while ( true ){

   if ( _kbhit() ) {
   /* Use _getch to throw key away. */
   printf( "\nKey struck was '%c'\n", _getch() );

   } else {

    // other operation

(If you are not using a ;S compiler, remove the trailing underscores :o)

LVL 86

Expert Comment

ID: 8065957
Ooops, the last sentence should have read

(If you are not using a MS compiler, remove the trailing underscores :o)

Expert Comment

ID: 8067011
>> (If you are not using a MS compiler, remove the trailing underscores :o)

kbhit() as well as getch() and other C functions can be used on Microsoft compilers without the underscores. At least I can do so and I am using MSVC++ v6.0

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 8069297
this may not help u in your 'game-input-programming'
but this is an example how can u specify timeouts and abort the getchar()..
this shud work on all UNIX

#include <stdio.h>
#include <setjmp.h>
#include <signal.h>
#include <unistd.h>
jmp_buf env;
static void signal_handler();

main()  {
         int returned_from_longjump, processing = 1;
         int c;
         unsigned int time_interval = 4;
         if ((returned_from_longjump = setjmp(env)) != 0)
                 switch (returned_from_longjump)     {
                        case SIGALRM:
                         printf("longjumped from alarm %d\n",SIGALRM);
                (void) signal(SIGINT, signal_handler);
                (void) signal(SIGALRM, signal_handler);
                printf(" will wait for u to enter character for 4 seconds ...\n"
                printf(" got character within 4 seconds . %c\n",c);


        printf("finishing up ..exiting\n");

static void signal_handler(sig)
int sig; {
         switch (sig)     {
           case SIGALRM:

           default:       exit(sig);

Expert Comment

ID: 8069308
as an option for your game,
 u can have separate threads for "display" and "input"
LVL 86

Expert Comment

ID: 8138366
Anything new on this?

Author Comment

ID: 8444524
It works in a way... I decided that OpenGL looked nicer, and our DOS console game (imitating atari or something) got trashed for a version of 3D Pong...

Thanks for the help!

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses
Course of the Month10 days, 8 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question