delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/20/21:06:17

From: mert0407 AT sable DOT ox DOT ac DOT uk (George Foot)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: INKEY in DJGPP? (Timed input function?)
Date: 20 Jun 1997 08:44:38 GMT
Organization: Oxford University, England
Lines: 36
Distribution: world
Message-ID: <5odftm$9mt@news.ox.ac.uk>
References: <5OUMnDAOyUqzEwjg AT netbook DOT demon DOT co DOT uk>
NNTP-Posting-Host: sable.ox.ac.uk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

James MacDonald (trill AT Xnetbook DOT demon DOT co DOT uk) wrote:

: Is it possible to have a three second delay in which time F1 can be
: pressed (like INKEY/INKEY$ in BBC BASIC)?

How about this:

#include <conio.h>
#include <time.h>

int inkey(int a)
/* IIRC a is measured in centiseconds, so that's what I've done here */
{
 int start;
 if (a<0)
  return 0; /* you'd need to use Allegro or similar to do these */
 else {
  start=clock();
  while ((!kbhit())&&(clock()-start<a*CLOCKS_PER_SEC/100));
  if (kbhit())
   return (getch());
  else
   return 0; /* or was it -1? Can't remember... I think it was 0 for
                false though */
 }
}

I tried this, it seems to work as expected. In C I suppose INKEY and
INKEY$ are pretty much the same. If you're fussy about timing accuracy,
replace clock with uclock in both places and replace CLOCKS_PER_SEC with
UCLOCKS_PER_SEC, but refer to the warnings in the documentation for
uclock.

-- 
George Foot <mert0407 AT sable DOT ox DOT ac DOT uk>
Merton College, Oxford

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019