Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Catch event's time

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

610 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