2
votes

I'm running AHK on Windows 10 to be able to use CapsLock for language switching (it's not the only purpose, though).

Here's the relevant part of the script:

SetCapsLockState, AlwaysOff
+CapsLock::CapsLock

*CapsLock::Send, {ALTDOWN}{SHIFTDOWN}{SHIFTUP}{ALTUP}

The shortcut works kind of unreliably, like it wouldn't switch the language once every three or four times, or something like this.

What could be the problem here?

Thanks!

2

2 Answers

0
votes

Try it like so:

SetCapsLockState, off  ;  may work with SetCapsLockState, AlwaysOff
+CapsLock::  ;  Shift CapsLock toggles CapsLock state
    if GetKeyState("CapsLock", "T") = 1
        SetCapsLockState, off
    else if GetKeyState("CapsLock", "F") = 0
        SetCapsLockState, on
return

*CapsLock::Send, {ALTDOWN}{SHIFTDOWN}{SHIFTUP}{ALTUP}

EDIT: Hmmm. I had problems at first with +CapsLock not toggling. Try this:

SetCapsLockState, off  ;  may work with SetCapsLockState, AlwaysOff
+CapsLock::  ;  Shift CapsLock toggles CapsLock state
    if GetKeyState("CapsLock", "T") = 1
        {
        MsgBox CapsLock going off
        SetCapsLockState, off
        }
    else if GetKeyState("CapsLock", "F") = 0
        {
        MsgBox CapsLock going on
        SetCapsLockState, on
        }
return

*CapsLock::
    MsgBox CapsLock pressed without shift
    Send, {ALTDOWN}{SHIFTDOWN}{SHIFTUP}{ALTUP}
return

And see if the capslock MsgBox is triggered by pressing shift+capslock

HTH, Let us know, YMMV

-1
votes

This is from somewhere on the internet:

SetCapsLockState, off
+Capslock::CapsLock
*Capslock::LangSwitch(1)
*Capslock up::LangSwitch(1)

LangSwitch( iKeyDownUp=0 )
{
    static tickLast
    IfEqual,iKeyDownUp,1
    {   tickLast=%A_TickCount%
        return
    }
    IfEqual,iKeyDownUp,2
        If( A_TickCount-tickLast>200 )
            return

    HKL:=DllCall("GetKeyboardLayout", "uint",GetThreadOfWindow(), "uint")

    HKLnum:=DllCall("GetKeyboardLayoutList","uint",0,"uint",0)
    VarSetCapacity( HKLlist, HKLnum*4, 0 )
    DllCall("GetKeyboardLayoutList","uint",HKLnum,"uint",&HKLlist)
    loop,%HKLnum%
    {   if( NumGet( HKLlist, (A_Index-1)*4 ) = HKL )
        {   HKL:=NumGet( HKLlist, mod(A_Index,HKLnum)*4 )
            break
        }
    }
    ControlGetFocus,ctl,A
    SendMessage,0x50,0,HKL,%ctl%,A ;WM_INPUTLANGCHANGEREQUEST

    ;show traytip
    LOCALE_SENGLANGUAGE=0x1001
    LOCALE_SENGCOUNTRY=0x1002
    VarSetCapacity( sKbd, 260, 0 )
    VarSetCapacity( sCountry, 260, 0 )
    DllCall("GetLocaleInfo","uint",HKL>>16,"uint",LOCALE_SENGLANGUAGE, "str",sKbd, "uint",260)
    DllCall("GetLocaleInfo","uint",HKL & 0xFFFF,"uint",LOCALE_SENGCOUNTRY, "str",sCountry, "uint",260)
    traytip,%sKbd%,%sCountry%
    SetTimer,REMOVE_TOOLTIP,500 ;0.5 second
    return
REMOVE_TOOLTIP:
    SetTimer,REMOVE_TOOLTIP,off
    traytip
    return
}