I run into an error says:
Code: Select all
Error: Invalid callback function.
016: DllCall("RegisterShellHookWindow", "UInt", Hwnd)
017: MsgNum := DllCall("RegisterWindowMessage", "Str", "SHELLHOOK")
▶ 018: OnMessage(MsgNum, ShellMessage)
019: Return
021: {
Code: Select all
Error: Target window not found.
Specifically: ahk_id 8
019: Return
021: {
▶ 022: title := WinGetTitle("ahk_id " lParam)
024: If (wParam = 4 || wParam = 32772)
024: {
This is the script:
Code: Select all
; =====================================================================
; You can use the function at the end of the code to find the ID of the current keyboard layout language.
; Cultures can be fetched from here: https://msdn.microsoft.com/en-us/library/windows/desktop/dd318693(v=vs.85).aspx
; They must be set twice in the language ID;
; Ar-TN: "0xf0291c01" this is the ID for the Arabic Tunisia keyboard layout language.
; En-US: "0x4090409" this is the ID for the English US keyboard layout language.
global DefaultLanguage := "Ar-TN"
global DefaultLanguageIndentifier := "0xf0291c01"
global SecondaryLanguage := "En-US"
global SecondaryLanguageIndentifier := "0x4090409"
global SecondaryLanguageWindowTitles := "Visual Studio"
; And the code itself (you should not have to change this)
Gui +LastFound
hWnd := WinExist()
DllCall( "RegisterShellHookWindow", UInt,Hwnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage" )
Return
ShellMessage( wParam,lParam )
{
WinGetTitle, title, ahk_id %lParam%
; 4 is HSHELL_WINDOWACTIVATED, 32772 is HSHELL_RUDEAPPACTIVATED
If (wParam=4 || wParam=32772) {
If title contains %SecondaryLanguageWindowTitles%
SetKeyboard( title, SecondaryLanguage )
Else
SetKeyboard( title, DefaultLanguage )
}
}
SetKeyboard( title, culture )
{
; 0x50 is WM_INPUTLANGCHANGEREQUEST.
Try
{
If (culture = SecondaryLanguage)
{
PostMessage, 0x50, 0, %SecondaryLanguageIndentifier%,, A
; To debug:
; ToolTip, Using secondary language %SecondaryLanguage%
; Sleep 1000
; ToolTip
}
Else (culture = DefaultLanguage)
{
PostMessage, 0x50, 0, %DefaultLanguageIndentifier%,, A
; To debug:
; ToolTip, Using default language %DefaultLanguage%
; Sleep 1000
; ToolTip
}
}
}
; .....................................................................
; You can use this to find the id of the current language:
^i::MsgBox, % GetDefaultKeyboard()
; get default keyboard language
GetDefaultKeyboard() {
ThreadID := DllCall("GetWindowThreadProcessId", "UInt", WinExist("A"), "UInt", 0)
InputLocaleID := DllCall("GetKeyboardLayout", "UInt", ThreadID, "UInt")
return Format("{:#x}", InputLocaleID)
}
; =====================================================================