Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help! Posted by Andris91 on 4 Nov 2009 at 7:35 AM
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?
Report
Re: Help! Posted by Atex on 4 Nov 2009 at 8:29 PM
: 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.


Report
Re: Help! Posted by Andris91 on 5 Nov 2009 at 5:49 AM
Thx for the help... but it seems like my pascal does not have a unit called timer...
Report
Re: Help! Posted by Atex on 5 Nov 2009 at 9:13 AM
: Thx for the help... but it seems like my pascal does not have a unit
: called timer...
:

That's why I posted the unit also (is right below the program)



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.