#Requires AutoHotkey v2.0
#SingleInstance force ; Ensures only one instance of this script runs

EnglishUS := 0x0409
Russian := 0x0419
Greek := 0x0408

GetKeyboardLayout()
{
    hwnd := WinGetID("A")
    threadID := DllCall("GetWindowThreadProcessId", "Ptr", hwnd, "Ptr", 0, "UInt")
    hkl := DllCall("GetKeyboardLayout", "UInt", threadID, "Ptr")
    return hkl
}

ChangeLanguage(LangID)
{
    ; WM_INPUTLANGCHANGEREQUEST = 0x0050
   
    try {
        ; Try to get the specific control (e.g., the Find text box) that has focus
        focusedControl := ControlGetFocus("A")
       
        ; Send the message directly to that control
        PostMessage(0x0050, 0, LangID, focusedControl, "A")
    }
    catch {
        ; If no specific control is found, fallback to sending it to the window itself
        PostMessage(0x0050, 0, LangID,, "A")
    }
}

LanguageCycle()
{
    langID := GetKeyboardLayout() & 0xFFFF
    switch langID
    {
        case EnglishUS:
            ; --- English (US) action ---
            ChangeLanguage(Russian)
           
        case Russian:
            ; ToolTip "Russan detected"
            ; --- Russian action ---
            ChangeLanguage(EnglishUS)
           
        case Greek:
            ; --- Greek action ---
            ChangeLanguage(EnglishUS)
           
        default:
            ToolTip "Other language: " Format("0x{:X}", langID)
    }
}

<!Shift::
{
    LanguageCycle()
}

<+Alt::
{
    LanguageCycle()
}