Solved

Catch event's time

Posted on 2004-08-10
5
230 Views
Last Modified: 2010-04-05
Hello everybody!!

I am using a a timer and I have problems to catch into a variable the time that an event takes. I just want to know how long a key is pressed.

Thanx.
0
Comment
Question by:Robertonio
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 4

Expert Comment

by:Colin_Dawson
ID: 11766943
How precisly do you need to know?  The TTimer isn't what you need for this...

For crude timings you can use  GetTickCount.  Call it in the OnKeydown, then again in the OnKeyUp.  The difference between the two times is the number of Milliseconds.   It's only accurate to about 50ms though.

For Really accurate timings use QueryPerformanceCounter and QueryPerformanceFrequency.  The Accuracy is dependant on the machine, but allows for nanosecond timing depending on the speed of the processor.
0
 
LVL 1

Author Comment

by:Robertonio
ID: 11767138
Thanx Colin !!

I'm working in a keyboard piano. I wrote a procedure that produces a wave and requires two values, both are Integer, frequency and duration(duration equals to the time that a key is pressed) . I need the duration in miliseconds in order to produce a wave with that timing, it should begin to sound OnKeyDown and it finishes onKeyUp.

0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 11770141
 TForm1 = class(TForm)
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    time_start   : Longint;
    time_end     : Longint;
  public
    { Public declarations }
  end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  time_start := GetTickCount;
end;

procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  time_end   := GetTickCount;
end;

Now when you like, you can see the difference between time_end and time_start. This is the time the key was pressed. Like Colin_Dawson said, for high performance you can use QueryPerformanceCounter.
0
 
LVL 7

Accepted Solution

by:
DavidBirch2dotCom earned 50 total points
ID: 11770705
As suggested above you can use QueryPerformanceCounter, here is a procedure I use to time functions,

You will need to add Msystem to the uses

var
  start,stop1,stop2,freq:int64;
  i,j:integer;
  Times: integer;
  TotalTicks,NumASec: int64;
  Ticksfor1,Timefor1: real;
  x:extended;
begin
  Times:= spinedit1.value;;

  screen.cursor:=crHourGlass;
  QueryPerformanceFrequency(freq); {Get counts/second}
  QueryPerformanceCounter(start); {Get string count}
  x:=0;
  for i:=1 to Times do ;
  QueryPerformanceCounter(stop1);  {Get end of loop1 count}
  for i:=1 to Times do {Test Procedure here ->}

  QueryPerformanceCounter(stop2); {Get end of loop2 count}
  screen.cursor:=crDefault;
  {calculate net counts per million processmessage calls and
   if we divide by freq (counts/sec), we get secs per million calls
  (i.e. microsecs per call)
 }

  TotalTicks:= (stop2-stop1)-(stop1-start);
  Ticksfor1:= TotalTicks/Times;
  Timefor1:= Ticksfor1 / freq;
  NumASec:= Trunc(1{second} / Timefor1);

  If freq>0 then
     showmessage('x='+floattostr(x)
     +#13+'Ticks taken to call the procedure '+inttostr(Times)+' times:= '+
     inttostr(TotalTicks)+
     #13+'      Ticks for 1 Call:= '+FloatToStrF(Ticksfor1,ffNumber, 4,2)+
     #13+'      Ticks per second:= '+inttostr(freq)+
     #13+'      Time in micro seconds for 1 call:= '+FloatToStrF(1e6*Timefor1,ffNumber, 4,8)+
     #13+'      Therefore you can call this procedure '+floattostr(NumASec)+' times a second')
  else showmessage('No hardware timer available');

David

0
 
LVL 7

Expert Comment

by:DavidBirch2dotCom
ID: 11770757
Add this code for the on key down and on key up events of an edit box or something, add MMsystem and maths to the uses


// on key down
var
  start,stop1,freq:int64;
  TotalTicks,NumASec: int64;
  Ticksfor1,Timefor1: real;

begin

   screen.cursor:=crHourGlass;
  QueryPerformanceFrequency(freq); {Get counts/second}
  QueryPerformanceCounter(start); {Get string count}


  QueryPerformanceCounter(Stop1); {Get end of loop2 count}
  screen.cursor:=crDefault;


///  on key up

 TotalTicks:= (stop1-start);
  Timefor1:= TotalTicks / freq;
  NumASec:= Trunc(1{second} / Timefor1);


  If freq>0 then
     showmessage('Ticks taken to call the procedure := '+
     inttostr(TotalTicks)+
     #13+'      Ticks for 1 Call:= '+FloatToStrF(Ticksfor1,ffNumber, 4,2)+
     #13+'      Ticks per second:= '+inttostr(freq)+
     #13+'      Time in micro seconds for 1 call:= '+FloatToStrF(1e6*Timefor1,ffNumber, 4,8)+
     #13+'      Therefore you can call this procedure '+floattostr(NumASec)+' times a second')
  else showmessage('No hardware timer available');


works nicely on my machine, microseconds are millionths of a second so it should be accurate enough ;-p)

DAvid
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question