: This may seem like a stupid question but I want to allow the user to
: input something like s:char; s:=readkey; within a time limit.
: If the user pressed something withing... lets say 10 seconds then
: s:=readkey, but if pascal has been waiting for an input for more the
: 10 seconds then the program should stop asking for that variable.
: How can this be done?
:
Here's one solution suitable for rough timing, the resolution being 1/18.2 secs, hooks on Int28:
{Compiler TP/BP 7}
program timed_input;
uses timer,crt;
var s:char;
trig:boolean;
begin
setup_timer;
writeln('Press Esc to quit. Setting timer to 10 sec',#13#10);
repeat
_timer_[1]:=182; { set timer to roughly 10 secs }
trig:=false;
writeln(#13#10,'Awaiting input...');
repeat
if keypressed then begin
s:=readkey;
_timer_[1]:=0;
trig:=true;
end;
until (_timer_[1]=0);
if trig then writeln(' ',s,' was pressed. Setting timer to 10 sec')
else writeln('Time''s up ! Resetting timer to 10 sec');
until s=#27;
shutdown_timer;
end.{Countdown timer functions}
unit timer;
interface
uses dos;
var _timer_:array[1..9] of longint; {1..8 user, 9 counts how many times the }
{interrupt was called }
old_28int:procedure;
procedure setup_timer;
procedure shutdown_timer;
procedure __timer__;interrupt;
implementation
{$f+}
procedure __timer__; {This interrupt is called 18.2 times/second}
var i:byte; {Not so precise, but simple :) }
begin
for i:=1 to 8 do
if _timer_[i]>0 then dec(_timer_[i]);
inc(_timer_[9]);
inline($9c); {pushf <-- push flags}
old_28int;
end;
{$f-}
procedure setup_timer;
begin
_timer_[9]:=0;
getintvec($1c,@old_28int);
setintvec($1c,addr(__timer__));
end;
procedure shutdown_timer;
begin
setintvec($1c,addr(old_28int));
end;
begin
_timer_[1]:=0; { 18.2 <-> 1 Sec}
_timer_[2]:=0;
_timer_[3]:=0;
_timer_[4]:=0;
_timer_[5]:=1092; { 1 min } { Preset values }
_timer_[6]:=3276; { 3 min }
_timer_[7]:=5460; { 5 min }
_timer_[8]:=10920; { 10 min }
_timer_[9]:=0;
end.