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

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
mexican scientist
Posts: 33
Joined: 02 Jul 2020, 21:55

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

14 Dec 2023, 19:10

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.]
User avatar
mikeyww
Posts: 27191
Joined: 09 Sep 2014, 18:38

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

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.
mexican scientist
Posts: 33
Joined: 02 Jul 2020, 21:55

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

14 Dec 2023, 19:58

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.
User avatar
mikeyww
Posts: 27191
Joined: 09 Sep 2014, 18:38

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

14 Dec 2023, 20:30

What is your belief about the role of the tilde in your hotkey, and why the tilde is necessary?
mexican scientist
Posts: 33
Joined: 02 Jul 2020, 21:55

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

14 Dec 2023, 20:54

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.
User avatar
mikeyww
Posts: 27191
Joined: 09 Sep 2014, 18:38

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

14 Dec 2023, 21:01

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

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: peter_ahk and 185 guests