2
votes

Problem

When a 3 key hotkey (LShift+CapsLock+LButton) is pressed, the a similar 2 key hotkey (CapsLock+LButton) is triggered instead.

Questions

  1. Why is this happening?

  2. How can the correct message box (shift left click) be triggered for the LShift+CapsLock+LButton hotkey?

Code

SetCapsLockState, AlwaysOff
CapsLock::
    MsgBox, "capslock"
return
CapsLock & LButton::
    MsgBox, "left click"
return

; 3 key hotkey
; problem here is when LShift+CapsLock+LButton are pressed, the CapsLock+LButton is 
; triggered instead
#If GetKeyState("LShift", "P") && GetKeyState("CapsLock", "P")
    LButton::
        MsgBox, "shift left click"
    return
#If

Example scenario

  • Press CapsLock, and correct message box (capslock) appears
  • Press CapsLock+LButton, and correct message box (left click) appears
  • Press LShift+CapsLock+LButton, and incorrect message box (left click) appears (message box (shift left click) should have appeared)
  • Press CapsLock+LShift+LButton, and incorrect message box (left click) appears (message box (shift left click) should have appeared)

What I've tried

  • Checking the #If directive documentation, it mentions a timeout that could affect the button combination, however, pressing the buttons at varying speeds still causes the CapsLock+LButton hotkey to trigger
  • Using nested regular if, else if, & else statements instead of the #If directive to alter the order of the 3 key hotkey
  • Using $ modifer to prevent the 3 key hotkey triggering the CapsLock+LButton hotkey
  • Breaking the 3 key hotkey into parts, e.g., putting a 2 key hotkey within a #If GetKeyState, i.e.:

    #If GetKeyState("LShift", "P")
        CapsLock & LButton::
            MsgBox, "shift left click"
        return
    #If
    

    This usually gives the correct message (shift left click) box but sometimes triggers the CapsLock hotkey giving the incorrect message box (capslock)

1
What about if (GetKeyState("LShift", "P")) in the CapsLock & LButton hotkey?Josh Brobst
@JoshBrobst I tried (pastebin) if (GetKeyState("LShift", "P")) in CapsLock & LButton with #If and regular if/else both stop CapsLock & LButton & the 3 key hotkey firing their MsgBoxsurajs02
Oh I mean using the true part of that if-statement to implement the 3-key hotkey, like thisJosh Brobst
@JoshBrobst Excellent idea, that works & is more stable than my previous solutions (when LShift is the last key released after the hotkey is triggered, e.g., LShift down+CapsLock down then CapsLock up+LShift up correctly doesn't trigger CapsLock hotkey but CapsLock hotkey is triggered if CapsLock is the last key released after the hotkey is triggered)surajs02
@JoshBrobst Can you put your solution as an answer to this question so it can be accepted so others with a similar problem can find it faster?surajs02

1 Answers

2
votes

Instead of having a separate 3-key hotkey, you can test for shift inside the 2-key hotkey to implement both CapsLock + LButton and CapsLock + Shift + LButton:

SetCapsLockState, AlwaysOff

CapsLock::
    MsgBox, "capslock"
return

CapsLock & LButton::
    if (GetKeyState("LShift", "P")) {
        MsgBox, "shift left click"
    }
    else {
        MsgBox, "left click"
    }
return

Avoiding code duplication for similar hotkeys:

  • Using a function

    CapsLock & RButton::CapsMouseHandler("right")
    CapsLock & LButton::CapsMouseHandler("left")
    
    CapsMouseHandler(btnString) {
        if (GetKeyState("LShift", "P")) {
            MsgBox % "shift " btnString " click"
        }
        else {
            MsgBox % btnString " click"
        }
    }
    
  • Using A_ThisHotkey

    CapsLock & RButton::
    CapsLock & LButton::
        btnString := InStr(A_ThisHotkey, "LButton") ? "left" : "right"
        if (GetKeyState("LShift", "P")) {
            MsgBox % "shift " btnString " click"
        }
        else {
            MsgBox % btnString " click"
        }
    return