How do you check for extra keys when LShift is set as the hotkey?

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

How do you check for extra keys when LShift is set as the hotkey?

16 Apr 2024, 07:51

Here is my minimul code:

Code: Select all

~Shift:: {
    pressTick := A_TickCount
    KeyWait("Shift", 300)
    if (A_TickCount - pressTick >= 300)
        return
    MsgBox("Shift key pressed")
}
The result I expected was that the function was triggered when only a short Shift press was pressed, and not if an extra key was pressed.
For example, in the case of the combination of keys such as Shift+A Shift+B.

Is there any other more concise way to deal with it besides checking whether it is pressed through more than 100 keys?
User avatar
mikeyww
Posts: 27012
Joined: 09 Sep 2014, 18:38

Re: How do you check for extra keys when LShift is set as the hotkey?

16 Apr 2024, 08:00

Welcome to this AutoHotkey forum!

I wonder how you found that syntax for KeyWait. It does not match the documentation.
T: Timeout (e.g. T3). The number of seconds to wait before timing out and returning 0. If the key or button achieves the specified state, the function will not wait for the timeout to expire. Instead, it will immediately return 1.
Source: KeyWait - Syntax & Usage | AutoHotkey v2
Check for other key on key up:

Code: Select all

#Requires AutoHotkey v2.0

~LShift Up:: {
 If A_PriorKey = 'LShift'
  MsgBox
}
Check for other key on key down:

Code: Select all

#Requires AutoHotkey v2.0
ih := InputHook('VL1T.3')

~LShift Up::ih.Stop
~LShift:: {
 ih.Start(), ih.Wait()
 ToolTip ih.EndReason
 If ih.EndReason = 'Timeout'
  MsgBox
}
Giresharu
Posts: 6
Joined: 16 Apr 2024, 07:21
Contact:

Re: How do you check for extra keys when LShift is set as the hotkey?

16 Apr 2024, 08:39

@mikeyww

Oh, I wrote the KeyWait parameter wrong, now I changed it to KeyWait("Shift") without timeout.

Also, thanks for telling me about A_PriorKey, which made my code work as expected.
But then I suddenly realized a little thing:

That is, A_PriorKey only returns A single key, and if I extend this script to ~ shift & a, I don't seem to be able to use (A_PriorKey == "a") to detect it, because when I press ctrl+shift+A, The return value of (A_PriorKey == "a") will also be true.
How should we deal with this situation?

And is there any advantage to InputHook over using Keywait and then determining the timeout? It seems a little more complicated
User avatar
mikeyww
Posts: 27012
Joined: 09 Sep 2014, 18:38

Re: How do you check for extra keys when LShift is set as the hotkey?

16 Apr 2024, 10:39

If the InputHook script is not achieving your goal, then your goal is not clear, at least not clear to me. If you have a revised script, then you should post it, describe what it does, and describe what it should do differently to achieve your goal.

KeyWait does not tell you whether a different key is pressed at any time. It tells you only whether the target (via parameter) of the KeyWait function is pressed or released. InputHook can be used to determine whether a different key, among many possibilities, is pressed while your hotkey itself is still being held. It seemed to me that this was your goal, but I am uncertain.

If you want to look for just a few combinations, this could be accomplished with a custom combination, while understanding the limitations of custom combinations.

Thus, there is not necessarily one right answer, but tradeoffs to be considered in pursuit of whatever your goal may be.

What the current InputHook script actually does:
The function was triggered when only a short Shift press was pressed, and not if an extra key was pressed.
My recommendation is to run the InputHook script, and see whether it achieves your goal.
Giresharu
Posts: 6
Joined: 16 Apr 2024, 07:21
Contact:

Re: How do you check for extra keys when LShift is set as the hotkey?

16 Apr 2024, 13:06

@mikeyww

Sorry, I was confused by some of the problems with this somewhat difficult api, so I didn't notice that InputHook can solve the problem of extra keys.

But I still don't know the correct usage of InputHook:
I tweaked the If statement in your code to call MsgBox when it's not Timeout, but it failed.
It seems that `ih.Stop` was forced while LShift Up so the Timeout status was changed to Stopped.

Code: Select all

#Requires AutoHotkey v2.0
ih := InputHook('VL1T.3')

~LShift Up:: ih.Stop()

~LShift:: {
    ih.Start(), ih.Wait()
    ToolTip ih.EndReason
    If ih.EndReason = 'Timeout'
        return
    MsgBox
}
Then it occurred to me that maybe I could check to see if it was Timeout while LShift Up, if it so, skip the `ih.Stop()`
As a result of my debugging, `ih.EndReason` became empty string when LShift Up, even though the ToolTip displayed a Timeout.
So it still always trigger the MsgBox.

Here's my new code:

Code: Select all

#Requires AutoHotkey v2.0
ih := InputHook('VL1T.3')

~LShift Up:: {
    If ih.EndReason != 'Timeout'
        ih.Stop()
}
~LShift:: {
    ih.Start(), ih.Wait()
    ToolTip ih.EndReason
    If ih.EndReason = 'Timeout'
        return
    MsgBox
}
User avatar
mikeyww
Posts: 27012
Joined: 09 Sep 2014, 18:38

Re: How do you check for extra keys when LShift is set as the hotkey?

16 Apr 2024, 18:26

Call MsgBox when it's not Timeout
If you do not need the "~LShift Up" hotkey, you can remove it completely.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Descolada and 56 guests