Can I determine if the Always property has been used in SetCapsLockState? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
Seven0528
Posts: 497
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Can I determine if the Always property has been used in SetCapsLockState?

Post by Seven0528 » 15 May 2024, 15:42

 Does anyone have a good idea for achieving this without resorting to sending CapsLock and observing the reaction?
I'm hoping for something like a magic trick using SendMessage to the main AHK script or similar methods.
(If not, I'll have to trace every instance of SetCapsLockState in tens of thousands of lines of code. Dreadful...)
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.

User avatar
mikeyww
Posts: 28870
Joined: 09 Sep 2014, 18:38

Re: Can I determine if the Always property has been used in SetCapsLockState?

Post by mikeyww » 15 May 2024, 20:08

I would actually recommend using the script as in your last comment, because the script "knows". That approach is direct, straightforward, and reliable.

lexikos
Posts: 9780
Joined: 30 Sep 2013, 04:07
Contact:

Re: Can I determine if the Always property has been used in SetCapsLockState?  Topic is solved

Post by lexikos » 16 May 2024, 03:54

You do not need to "trace every instance of SetCapsLockState". Off the top of my head, you could:
  • Define your own MySetCapsLockState function and use search and replace to replace calls in your script, no "tracing" needed. Your function can call the built-in one before/after recording whether the Always mode was used.
  • Define your own SetCapsLockState function and ignore the built-in one. You can implement it with GetKeyState to check the current toggle state, Send to toggle it if appropriate, and Hotkey to register/enable/disable a hotkey to suppress the key, or InputHook to suppress the key. The rest of your script can continue to call SetCapsLockState as before.
  • Hook SetCapsLockState by redefining the Call property. Whenever SetCapsLockState is called, your code will run first and can record whether the Always mode was used.

Code: Select all

#Requires AutoHotkey v2.0
global CapsLockAlways := ""
SetCapsLockState.DefineProp('Call', {call: _SCLS_Call})
_SCLS_Call(scls, state) {
    (scls.base.Call)(scls, state)
    global CapsLockAlways := SubStr(state,7)
}

MsgBox CapsLockAlways
SetCapsLockState "AlwaysOn"
MsgBox CapsLockAlways
SetCapsLockState "AlwaysOff"
MsgBox CapsLockAlways

Code: Select all

#Requires AutoHotkey v2.1-alpha.5
global CapsLockAlways := ""
SetCapsLockState.DefineProp('Call', {call: (scls, state) {
    (scls.base.Call)(scls, state)
    global CapsLockAlways := SubStr(state,7)
}})

MsgBox CapsLockAlways
SetCapsLockState "AlwaysOn"
MsgBox CapsLockAlways
SetCapsLockState "AlwaysOff"
MsgBox CapsLockAlways

Post Reply

Return to “Ask for Help (v2)”