Time between keystrokes?

Status
Not open for further replies.

technophile

Ars Legatus Legionis
23,768
getc() or getch (I can never remember) will instantly return when a key is pressed (i.e. unlike most input functions, it does not block until EOF or EOL is reached).

So you get the system time, call getc(), and as soon as you return get the system time again. The difference is how long between keypresses.

e.g. (pseudocode)
code:<HR>
Code:
  getc(); /* blocks until a key is pressed */
  time1 = get_system_time();
  getc(); /* blocks until a key is pressed */
  time2 = get_system_time();
  printf("The time between keypresses was %d\n", time2 - time1);
 

bugbum1

Ars Scholae Palatinae
893
EgalitarianBovine
but unfortunately, most filestreams are 'cooked'.

yeah, but that doesn't stop you from looking in the buffer - heh, heh.

this works is both MS C and GCC ... (i'll let STL tell you if it's in the spec).
code:<HR>
Code:
   while( !kbhit() ); /* wait until something is in the buffer */
   {
       command = getch(); /* get it */
   }
you can use this approach to find the time between any two keystrokes (in a cooked stream) ;)

...and getc() and getch() will only return if something is in the buffer, otherwise they will block.
 
Status
Not open for further replies.