#define MILLISECONDS_FROM    0
#define MILLISECONDS_TO   1000
#define STEP_SIZE           10

#define SAMPLE_SIZE        100

proc Main()
  integer i                 = 0
  integer milliseconds      = 0
  integer old_DateFormat    = Set(DateFormat   , 6)
  integer old_DateSeparator = Set(DateSeparator, Asc('-'))
  integer old_TimeFormat    = Set(TimeFormat   , 3)
  integer old_TimeSeparator = Set(TimeSeparator, Asc('-'))
  integer t0                = 0
  integer t1                = 0
  if EditFile(SplitPath(CurrMacroFilename(), _DRIVE_|_PATH_|_NAME_)
              + '_' + StrReplace('-', GetDateStr(), '', '')
              + '_' + StrReplace('-', GetTimeStr(), '', '')
              + '.txt',
              _DONT_PROMPT_)
    EmptyBuffer()
    for milliseconds = MILLISECONDS_FROM to MILLISECONDS_TO by STEP_SIZE
      Message('Press <Escape> to stop. Testing'; milliseconds;
              'of'; MILLISECONDS_TO; '...')
      t0 = GetTime()
      for i = 1 to SAMPLE_SIZE
        WaitForKeyPressed(milliseconds)
      endfor
      t1 = GetTime()
      if KeyPressed()
        while KeyPressed()
          GetKey()
        endwhile
        break
      else
        AddLine(Format(SAMPLE_SIZE; 'times WaitForKeyPressed(',
                       milliseconds: Length(Str(MILLISECONDS_TO)),
                       ') averaged';
                       ((t1 - t0) * 10) / SAMPLE_SIZE: 4;
                       'milliseconds.'))
        UpdateDisplay()
      endif
    endfor
    SaveAs(CurrFilename(), _DONT_PROMPT_|_OVERWRITE_)
    Message('Ready.')
  else
    Warn('Opening log file failed.')
  endif
  Set(DateFormat   , old_DateFormat   )
  Set(DateSeparator, old_DateSeparator)
  Set(TimeFormat   , old_TimeFormat   )
  Set(TimeSeparator, old_TimeSeparator)
  PurgeMacro(SplitPath(CurrMacroFilename(), _NAME_))
end Main