0
votes

Can anyone spot the bug in this AutoHotKey program, which I am running in Windows 7?

~i::iDown := A_TickCount
~i up::MsgBox, % "down at " iDown ", up at " A_TickCount ", down for " A_TickCount - iDown " ms."

In one run, when I held the "i" key for about 5 seconds, I got the result: down at 25700712, up at 25700743, down for 31 ms. Each time I run the program, I get a different (seemingly) random result, usually less than 100 ms. The result doesn't seem to correspond to how long I actually hold down the key. The subtraction part seems to be correct, so there must be something wrong with my implementation of A_TickCount.

2

2 Answers

1
votes

Can't say for sure whats happening but seems to come from the keyboard drivers auto-repeat.

This will show you how it keeps recalling A_tickCount

i::
iDown := A_TickCount
tooltip %iDown%
return

i up::MsgBox, % "down at " iDown ", up at " A_TickCount ", down for " A_TickCount - iDown " ms."

And a workaround

~i::
iDown := A_TickCount
KeyWait, i
MsgBox, % "down at " iDown ", up at " A_TickCount ", down for " A_TickCount - iDown " ms."
return

Hope it helps

0
votes

From the manual:

A_TickCount; The number of milliseconds that have elapsed since the system was started

A_TickCount is a ReadOnly variable that contains the time (in milliseconds) the computer has been running.

From the manual:

StartTime := A_TickCount
Sleep, 1000
ElapsedTime := A_TickCount - StartTime
MsgBox,  %ElapsedTime% milliseconds have elapsed.