Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

Catch event's time

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
Robertonio
Asked:
Robertonio
1 Solution
 
Colin_DawsonCommented:
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
 
RobertonioAuthor Commented:
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
 
Ivanov_GCommented:
 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
 
DavidBirch2dotComCommented:
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
 
DavidBirch2dotComCommented:
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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now