bubyoz
bubyoz

Reputation: 25

How to reset a key sequence with a specific key?

So this sequence resets itself after 1.5 sec (t:=1500) which means if i dont hit the left mouse button for 1.5 sec it always sends A. Otherwise it sends the next letter after each click.

I want to further tweak this code with another function which is to be able to reset the sequence with right mouse button too. So if i hit RButton any time it should reset to A.

Thx.

global s:=0, c:=0, t:=1500

*LButton::
    Send % Seqkeys("A","B","C")
    KeyWait, LButton
    Send, R
return

Seqkeys(params*) { 
    global s, c, t
    max := params.MaxIndex()
    (A_TickCount-s<=t && (c+=1)<=max) ? c : c:=1
    s := A_TickCount
    return params[c]
}

Upvotes: 0

Views: 179

Answers (1)

Lifeweaver
Lifeweaver

Reputation: 1042

Merely reset the current key index 'c', and the last clicked time 's':

*RButton::
    c := 1
    s := 0
return

I think your script would benefit with more meaningful variable names:

global lastClickedTime:=0, currentKeyIndex:=0, clickThreshold:=1500

*LButton::
    Send % Seqkeys("A","B","C")
    KeyWait, LButton
    Send, R
return

*RButton::
    currentKeyIndex := 1
    clickThreshold := 0
return

Seqkeys(params*) { 
    global lastClickedTime, currentKeyIndex, clickThreshold
    max := params.MaxIndex()
    currentKeyIndex += 1

    if((A_TickCount - lastClickedTime) <= clickThreshold && currentKeyIndex <= max) {
        ; Do nothing
    } else {
        currentKeyIndex := 1
    }

    lastClickedTime := A_TickCount
    return params[currentKeyIndex]
}

Upvotes: 1

Related Questions