Page 1 of 1

Help with a simple code with time, its driving me nuts!

Posted: 14 Dec 2023, 19:10
by mexican scientist

Code: Select all

~Space:: ; Triggered when the Space key is pressed
    StartTime := A_TickCount ; Record the start time
    KeyWait, Space ; Wait for the Space key to be released
    ElapsedTime := A_TickCount - StartTime ; Calculate the elapsed time
    MsgBox, Time elapsed: %ElapsedTime% milliseconds
    return
The code works as expected. If the Space key on the keyboard is pressed, then the message box displays the time it took me to press the space key. HOWEVER, and here is where this is driving me nuts: I have a controller, a ps4 controller which I have mapped a key to "space" key. If I press the button on the controller, the code executes and shows the message box, but the elaped time is always shown as 0. Please help.

What must be done so that the time is recorded when I press the controller key that is linked to "space"


[Mod action: Moved topic to the v1 section since this is v1 code. The main section is now for v2.]

Re: Help with a simple code with time, its driving me nuts!

Posted: 14 Dec 2023, 19:41
by mikeyww
If your controller maps a key to Space, it might act by sending the key down and then immediately up.

Re: Help with a simple code with time, its driving me nuts!

Posted: 14 Dec 2023, 19:58
by mexican scientist
mikeyww wrote:
14 Dec 2023, 19:41
If your controller maps a key to Space, it might act by sending the key down and then immediately up.
I don't think it is sending the key down and then immediately up because on the controller If I press the mapped key for 1 second or longer, it behaves like a long press. The message box displays and shows 0.

The only way the code records the time is if I press Space on the keyboard.

Re: Help with a simple code with time, its driving me nuts!

Posted: 14 Dec 2023, 20:30
by mikeyww
What is your belief about the role of the tilde in your hotkey, and why the tilde is necessary?

Re: Help with a simple code with time, its driving me nuts!

Posted: 14 Dec 2023, 20:54
by mexican scientist
mikeyww wrote:
14 Dec 2023, 20:30
What is your belief about the role of the tilde in your hotkey, and why the tilde is necessary?
If I don't put that tilde, then the key is not registered in the video game that I am playing with the controller.

Re: Help with a simple code with time, its driving me nuts!

Posted: 14 Dec 2023, 21:01
by mikeyww
How about with $?

Code: Select all

#Requires AutoHotkey v1.1.33

$Space::
Send {Space}
start := A_TickCount
KeyWait Space
MsgBox 64, Elapsed time, % A_TickCount - start " ms"
Return
Or:

Code: Select all

#Requires AutoHotkey v1.1.33

Space Up::MsgBox 64, Elapsed time, % A_TickCount - start " ms"
Space::
Send {Space}
start := A_TickCount
KeyWait Space
Return
Or:

Code: Select all

#Requires AutoHotkey v1.1.33

$Space::
SetKeyDelay 30, 25
start := A_TickCount
While GetKeyState("Space", "P")
 SendEvent {Space}
MsgBox 64, Elapsed time, % A_TickCount - start " ms"
Return