Skip to content

Instantly share code, notes, and snippets.

@peterjs
Last active March 18, 2018 09:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterjs/8041753 to your computer and use it in GitHub Desktop.
Save peterjs/8041753 to your computer and use it in GitHub Desktop.
Change keyboard layout on getting/losing focus
;========================================================================
;
; Template: WinTrigger (former OnOpen/OnClose)
; Description: Act upon (de)activation/(un)existance of programs/windows
; Online Ref.: http://www.autohotkey.com/forum/viewtopic.php?t=63673
;
; Last Update: 15/Mar/2010 17:30
;
; Created by: MasterFocus
; http://www.autohotkey.net/~MasterFocus/AHK/
;
; Thanks to: Lexikos, for improving it significantly
; http://www.autohotkey.com/forum/topic43826.html#267338
;
;========================================================================
;
; This template contains two examples by default. You may remove them.
;
; * HOW TO ADD A PROGRAM to be checked upon (de)activation/(un)existance:
;
; 1. Add a variable named ProgWinTitle# (Configuration Section)
; containing the desired title/ahk_class/ahk_id/ahk_group
;
; 2. Add a variable named WinTrigger# (Configuration Section)
; containing the desired trigger ("Exist" or "Active")
;
; 3. Add labels named LabelTriggerOn# and/or LabelTriggerOff#
; (Custom Labels Section) containing the desired actions
;
; 4. You may also change CheckPeriod value if desired
;
;========================================================================
#Persistent
; ------ ------ CONFIGURATION SECTION ------ ------
; Program Titles
ProgWinTitle1 = ahk_class Calculator
WinTrigger1 = Exist
;ProgWinTitle2 = Calculator
ProgWinTitle2 = ahk_class Notepad
WinTrigger2 = Active
; SetTimer Period
CheckPeriod = 200
; ------ END OF CONFIGURATION SECTION ------ ------
;SetTimer, LabelCheckTrigger, %CheckPeriod%
GoSub LabelCheckTrigger
Return
; ------ ------ ------
LabelCheckTrigger:
;if (!MessageNotProcessed) {
;MessageNotProcessed := 1
while(1) {
Sleep 2000
LastState := 0
While ( ProgWinTitle%A_Index% != "" && WinTrigger := WinTrigger%A_Index% )
;if ( !ProgRunning%A_Index% != !Win%WinTrigger%( ProgWinTitle := ProgWinTitle%A_Index% ) )
;GoSubSafe( "LabelTriggerO" ( (ProgRunning%A_Index% := !ProgRunning%A_Index%) ? "n" : "ff" ) A_Index )
;if ( !ProgRunning%A_Index% != !Win%WinTrigger%( ProgWinTitle := ProgWinTitle%A_Index% ) )
GoSubSafe( "LabelTriggerO" ( (!WinActive(ProgWinTitle%A_Index%)) ? "ff" : "n" ) A_Index )
}
Return
; ------ ------ ------
GoSubSafe(mySub)
{
if IsLabel(mySub)
GoSub %mySub%
;MessageNotProcessed := 0
}
; ------ ------ CUSTOM LABEL SECTION ------ ------
LabelTriggerOn1:
Return
LabelTriggerOff1:
Return
LabelTriggerOn2:
;MsgBox % "A_ThisLabel:`t" A_ThisLabel "`nProgWinTitle:`t" ProgWinTitle "`nWinTrigger:`t" WinTrigger
;DllCall("LoadKeyboardLayout", "Str", "00000409", "Int", 1)
if (LastState != 1)
{
SendMessage, 0x50,, 0x409,, A
;DllCall("LoadKeyboardLayout", "Str", "00000409", "Int", 257)
;MsgBox % "A_ThisLabel:`t" A_ThisLabel "`nProgWinTitle:`t" ProgWinTitle "`nWinTrigger:`t" WinTrigger
LastState := 1
Sleep 2000
}
; if ErrorLevel == FAIL
; {
; MessageNotProcessed := 0
; }
Return
LabelTriggerOff2:
;MsgBox % "A_ThisLabel:`t" A_ThisLabel "`nProgWinTitle:`t" ProgWinTitle "`nWinTrigger @@@@@:`t" WinTrigger
;DllCall("LoadKeyboardLayout", "Str", "00000419", "Int", 1)
if (LastState != 2)
{
SendMessage, 0x50,, 0x041B,, A
;DllCall("LoadKeyboardLayout", "Str", "0000041B", "Int", 257)
;MsgBox % "A_ThisLabel:`t" A_ThisLabel "`nProgWinTitle:`t" ProgWinTitle "`nWinTrigger @@@@@:`t" WinTrigger
LastState := 2
Sleep 1000
}
; if ErrorLevel == FAIL
; {
MessageNotProcessed := 0
; }
Return
; ------ END OF CUSTOM LABEL SECTION ------ ------
;F4::
;WinGetTitle, wintitle, A
;MsgBox %wintitle%
;return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment