Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3343
  • Last Modified:

getch() with timeout

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...

Thanks!
0
birdgenj
Asked:
birdgenj
1 Solution
 
jkrCommented:
'_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)

0
 
jkrCommented:
Ooops, the last sentence should have read

(If you are not using a MS compiler, remove the trailing underscores :o)
0
 
ExceterCommented:
>> (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

Exceter
0
Industry Leaders: 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!

 
akshayxxCommented:
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);
                         break;
                 }
         else{
                (void) signal(SIGINT, signal_handler);
                (void) signal(SIGALRM, signal_handler);
                printf(" will wait for u to enter character for 4 seconds ...\n"
                alarm(time_interval);
                c=getchar();
                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:
                                          longjmp(env,sig);

           default:       exit(sig);
         }
}
0
 
akshayxxCommented:
as an option for your game,
 u can have separate threads for "display" and "input"
0
 
jkrCommented:
Anything new on this?
0
 
birdgenjAuthor Commented:
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!
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now