?
Solved

Catch event's time

Posted on 2004-08-10
5
Medium Priority
?
231 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 150 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month9 days, left to enroll

764 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