Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to activate/deactivate AutoHotkey script based on active input method?

Tags:

autohotkey

I have an autohotkey script that I use only in conjunction with my Japanese input.

I only have one input language on my computer (Japanese), and I type in English by using the Alphanumeric setting (I'm not switching to English language, it's still technically a Japanese input method. But it's the Alphanumeric keyboard which has roman letters and is still a part of the Japanese language package on my computer). I toggle between Alphanumeric characters & Hiragana using the built-in hotkey Alt + `. Right now, my AHK script has a Suspend toggle which I activate with the combination Alt + 1.

I've tried changing the Suspend Toggle to Alt + grave, but if I do that, I can't toggle the script off. My computer changes input languages (as per the built-in hotkey), but does nothing to activate/deactivate the AHK script.

Right now, when I change languages, I have to do two hotkey combinations in a row (First Alt + grave, then Alt + 1). It's pretty redundant. I'd like for AHK to recognize which input method I'm using (Alphanumeric or Hiragana), and turn on or off based on that info. OR, if I could simply have the Alt + grave work to simultaneously toggle both the input & the state (active/unactive) of my AHK script.

For more--maybe unnecessary--details: When I have the hiragana keyboard on, pressing the "s" key outputs と, the Japanese "TO". Usually, Shift + s would still output と. I wrote my AHK script to change Shift + s to ど, or Japanese "DO".

As it is now, if I switch back to Alphanumeric characters and hit "s", I of course get "s" as desired. But if I tried to type a capital S, I still get a ど because the Shift + s is still mapped to that hiragana.

like image 774
user482110 Avatar asked Nov 28 '25 03:11

user482110


1 Answers

If, as you said, your computer changes input languages (as per the built-in hotkey), you can try this to activate/deactivate the AHK script:

SetTimer, SuspendScript, 500

    RETURN   ; === end of auto-execute section ===

; Your hotkeys here:
; ...

    SuspendScript:
If (GetKeyboardLanguage(WinActive("A")) = 0x0409) ; English
    Suspend Off
else
    Suspend On
Return   

; https://autohotkey.com/board/topic/116538-detect-which-language-is-currently-on/#entry672236
GetKeyboardLanguage(_hWnd=0){
    if !_hWnd
        ThreadId=0
    else
        if !ThreadId := DllCall("user32.dll\GetWindowThreadProcessId", "Ptr", _hWnd, "UInt", 0, "UInt")
            return false    
    if !KBLayout := DllCall("user32.dll\GetKeyboardLayout", "UInt", ThreadId, "UInt")
        return false    
    return KBLayout & 0xFFFF
}
like image 149
user3419297 Avatar answered Nov 29 '25 22:11

user3419297



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!