Search code examples
windowsautohotkeyauto

Can you fix my Ctrl+Left Ctrl+Shift+Left script?


It's only slightly not like I wanted it. It works fine, but if I want to hold down the functions it will perform actions once and add numbers instead of repeating the function. I wanted something to speed up the process of Ctrl+left arrow, and Ctrl+shift+left arrow, and Ctrl+Right Arrow & Ctrl+Shift+Right Arrow so that I can work with text faster. https://www.youtube.com/watch?v=VNRCQx0eWA0 You can view an example of what I am referring to here, and what's going wrong. I've marked the mistakes within the title of the video.

script.ahk script contents

^1::
Send ^{Left Down}^{Left Down}
return
^2::
Send ^{Left Down}^{Left Down}^{Left Down}
return
^3::
Send ^{Left Down}^{Left Down}^{Left Down}^{Left Down}
return
^4::
Send ^{Left Down}^{Left Down}^{Left Down}^{Left Down}^{Left Down}
return
^5::
Send ^{Left Down}^{Left Down}^{Left Down}^{Left Down}^{Left Down}^{Left Down}
return
^6::
Send ^{Right Down}^{Right Down}
return
^7::
Send ^{Right Down}^{Right Down}^{Right Down}
return
^8::
Send ^{Right Down}^{Right Down}^{Right Down}^{Right Down}
return
^9::
Send ^{Right Down}^{Right Down}^{Right Down}^{Right Down}^{Right Down}
return
^0::
Send ^{Right Down}^{Right Down}^{Right Down}^{Right Down}^{Right Down}^{Right Down}
return
^+1::
Send ^+{Left Down}^+{Left Down}
return
^+2::
Send ^+{Left Down}^+{Left Down}^+{Left Down}
return
^+3::
Send ^+{Left Down}^+{Left Down}^+{Left Down}^+{Left Down}
return
^+4::
Send ^+{Left Down}^+{Left Down}^+{Left Down}^+{Left Down}^+{Left Down}
return
^+5::
Send ^+{Left Down}^+{Left Down}^+{Left Down}^+{Left Down}^+{Left Down}^+{Left Down}
return
^+6::
Send ^+{Right Down}^+{Right Down}
return
^+7::
Send ^+{Right Down}^+{Right Down}^+{Right Down}
return
^+8::
Send ^+{Right Down}^+{Right Down}^+{Right Down}^+{Right Down}
return
^+9::
Send ^+{Right Down}^+{Right Down}^+{Right Down}^+{Right Down}^+{Right Down}
return
^+0::
Send ^+{Right Down}^+{Right Down}^+{Right Down}^+{Right Down}^+{Right Down}^+{Right Down}
return
#q::exitApp

Solution

  • Keys := ["1","2","3","4","5","6","7","8","9","0"]
    for each, key in Keys
    {
        Hotkey, ^%key%, Ctrl_Key, On 
        Hotkey, ^+%key%, Ctrl_Key, On
    }
    return
    
    Ctrl_Key:
        last_Key := ""
        Index := ""
        last_Key := SubStr(A_ThisHotkey, 0)
        If (last_Key = 0)
            Index = 5
        else
        If (last_Key < 6)
            Index := last_Key + 1
        else
            Index := last_Key - 4
        Loop %Index%
        {
            If (last_Key < 6) and (last_Key > 0)
            {
                If GetKeyState("Shift","P")
                    Send +^{Left}
                else
                    Send ^{Left}
            }
            else
            {
                If GetKeyState("Shift","P")
                    Send +^{Right}
                else
                    Send ^{Right}
            }
        }
        ; Don't repeat the action before key is released: 
        KeyWait, %last_Key%
    return
    

    See For-Loop, Hotkey and SubStr in the documentation and feel free to ask if you don't understand something.