Does the triggering of CapsLock's hotkey be faster than the change of the CapsLock state itself?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Giresharu
Posts: 17
Joined: 16 Apr 2024, 07:21
Contact:

Does the triggering of CapsLock's hotkey be faster than the change of the CapsLock state itself?

17 May 2024, 09:30

I currently have a requirement to record the CapsLock state for each process so that when I switch back, it can automatically restore. So, I want to implement a feature to record the CapsLock state when CapsLock is pressed. I tested it with the following code:

Code: Select all

~CapsLock:: {
    ToolTip(GetKeyState("CapsLock", "T") ? "On" : "Off")
}

~CapsLock Up:: {
    ToolTip(GetKeyState("CapsLock", "T") ? "On" : "Off")
}
As a result, when I pressed CapsLock to turn on CapsLock, the ToolTip displayed "Off", and vice versa, it displayed "On". So, I suspected it was a delay issue and modified the code for testing:

Code: Select all

~CapsLock:: {
    SetTimer(() => ToolTip(GetKeyState("CapsLock", "T") ? "On" : "Off"), -50)
}

~CapsLock Up:: {
    SetTimer(() => ToolTip(GetKeyState("CapsLock", "T") ? "On" : "Off"), -50)
}
This time it worked. When CapsLock is turned on, it displays "On", and vice versa, it displays "Off".

Although this met my expectations, I can't help but wonder, is this delay fixed? Does the required wait time differ on different PCs based on performance? In different environments, does AHK always faster than the CapsLock state actually change? When using Hotkeys, will it always get the state before the change occurs? Can I simply invert the states, treating "On" as "Off" and "Off" as "On" to ensure it works correctly without issues?
User avatar
Noitalommi_2
Posts: 321
Joined: 16 Aug 2023, 10:58

Re: Does the triggering of CapsLock's hotkey be faster than the change of the CapsLock state itself?

17 May 2024, 10:24

Hi.

Your first script works just fine on my PC.

However there might be an issue with "unusual keyboard drivers"
As the documentation says:
Systems with unusual keyboard drivers might be slow to update the state of their keys, especially the toggle-state of keys like CapsLock. A script that checks the state of such a key immediately after it changed may use Sleep beforehand to give the system time to update the key state.
Edit:
Can't test it because i don't have this issue but this might be a workaround.

Code: Select all

#Requires AutoHotkey 2.0
#SingleInstance


CapsLock:: {

	static Toggle := GetKeyState("CapsLock", "T")

	SetCapsLockState Toggle := !Toggle
	ToolTip Toggle ? "On" : "Off"
}
Giresharu
Posts: 17
Joined: 16 Apr 2024, 07:21
Contact:

Re: Does the triggering of CapsLock's hotkey be faster than the change of the CapsLock state itself?

17 May 2024, 10:29

@ Noitalommi_2
Oh, it looks like the problem might be with my keyboard.
Thank you for your answer!

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: bobstoner289, just me, mikeyww, Xtra and 29 guests