Solved

Catch event's time

Posted on 2004-08-10
5
224 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
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now