2

First, sorry for my bad English. I'm using GetTickCount() function included in windows.h and getch() included in conio.h.

What I precisely want is to give user a time limit to input char. If time limit lapses, program continues to execute, skipping the wait for user to input the char.

char ch='A';
DWORD start_time, check_time;

start_time=GetTickCount();
check_time=start+500; //GetTickCount returns time in miliseconds, so I add 500 to wait input for half a second.

while (check_time>GetTickCount()) {
ch=getchar();
}

//do stuff with char with initial value of 'A' if user didn't enter another char during 500ms of wait.

But getchar() stops executing the program and waits for user to input char for indefinite time. Is there an easy solution to bypass this wait, and continue if 500ms passed?

EDIT:

Based on your tips, i wrote this and it works! Thank you guys!

while (!kbhit()&&(check_time>GetTickCount()))
    {
        if (kbhit())
        {
            ch=getch();
            break;
        }
    }
Rtvi
  • 73
  • 2
  • 7
  • you must use another function for reading from stdin like ioctl, see this too http://stackoverflow.com/questions/10004895/c-reading-from-stdin-as-characters-are-typed – danics Nov 07 '14 at 18:10
  • On unix you have select() to do that job. On windows, I don't know. – Charlie Burns Nov 07 '14 at 18:17
  • ***[This link](http://stackoverflow.com/questions/448944/c-non-blocking-keyboard-input)*** shows how to do this – ryyker Nov 07 '14 at 18:18
  • @CharlieBurns - `GetAsyncKeyState()` is used in windows for detecting a specific key. Not sure its a perfect fit here, but works great for setting a specific key, seeing when it is hit. – ryyker Nov 07 '14 at 18:22
  • In your link there's something called kbdhit() that might do the trick for him. – Charlie Burns Nov 07 '14 at 18:24
  • You are aware you are taking a completly non Standard C approach? – alk Nov 07 '14 at 19:35
  • @alk: Since there is not a standard C approach for this, I'd say that's self evident. – indiv Nov 07 '14 at 19:38
  • @indiv: Fair enough ... – alk Nov 07 '14 at 19:49

2 Answers2

2

As proposed by Charlie Burns the kbhit function from conio does exactly what you want : it looks it a key was hit.

You could do :

DWORD start_time, check_time;

start_time=GetTickTime();
check_time=start+500; //GetTickTime returns time in miliseconds, so I add 500 to wait input for half a second.
char ch = 0;
char hit =0

while (check_time>GetTickTime()) {
    if (_kbhit()) {
        hit = 1;
        ch = _getch();
        if (ch  == 0) ch = _getch() // an arrow key was pressed
        break;
    }
}
// if hit == 0 we got a timout, else ch is the code of the key

Beware : untested ...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Yes, that solves the problem! I got the similar solution, but I think your might work better for some situations. For some reason I'm not allowed to up vote your post. – Rtvi Nov 07 '14 at 19:51
1

Windows solution using GetTickCount() and GetAsyncKeyState(...):

This approach uses the non-blocking Windows API function GetAsyncKeyState(), with GetTickCount (I do not have GetTimeTick) But that can easily be changed out for GetTickCount() for your system.

keyPressed(...) looks at each of the 256 keys, including virtual keys of a key board, and returns true if anything has been pressed:

#include <windows.h>  

BOOL keyPressed(char *keys)
{
    for(int i = 0; i<256; i++)
        if(GetAsyncKeyState(i) >> 8) return 1;
    return 0;   
}    

Test for keyPressed function:

#define TIME_LIMIT 10

int main(void)
{
    int c=0;
    char *key;  
    DWORD Start, Duration=0; //unsigned long int

    key = calloc(256, 1);

    Start = GetTickCount();

    memset(key, 0, 256);
    while((Duration < MAX_TIME)&&(!keyPressed(key))) // \n character
    {
        Duration = GetTickCount() - Start;
    }
    if (Duration < MAX_TIME) printf("in-time\n");
    else printf("Out of time\n");

    getchar();

    free (key);
    return 0;
}
ryyker
  • 22,849
  • 3
  • 43
  • 87