Automatically change the keyboard layout language on specific windows ???

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
yfjuu6
Posts: 125
Joined: 28 Apr 2023, 15:28

Automatically change the keyboard layout language on specific windows ???

11 May 2023, 14:14

- Most of the time I forget to change the keyboard layout language from Arabic to English when opening the VS code or any other editor, which forced me to change it manually every time I forgot to do so. For this reason I was looking for a script that automatically changes the keyboard layout language when specific windows get focus (Active) until I found this script on GitHub.
It works well when the window is not minimized, But there is a simple and weird problem when the window is minimized :
- For ex, when the specific window (Visual Studio Code) is Not Minimized the script performs well the desired operation, But when the window is minimized and the current keyboard layout language is Arabic (for ex), and I wanted to restore the window (by click on the task bar icon) and automatically the language changes to English, When I restore the window (by click on the task bar icon), the current input language does not change automatically from the first time (restoring), but when I minimize it again then restore it from the second time, the input language changes automatically to English.
- Could someone make the script do the desired operation from the first time (restoring) when the window is minimized?
- Another question, How do I put the Process name or the window class ... in stead of the window title?
- If there is any other script that performs the desired operation, please give it to me.
- Please help! I appreciate your time and effort. Thanks in advance.
- This is the Autohotkey 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)
 }
; =====================================================================
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Automatically change the keyboard layout language on specific windows ???

11 May 2023, 14:53

Hi, yfjuujy6

Step by step. Your script cannot be tested by someone else who does not have the two languages ​​you use in your script and the programs you reference.

Below is a script to switch between multiple languages, which references a function:

Code: Select all

^1::SetDefaultKeyboard(0x0406) ; Danish
^2::SetDefaultKeyboard(0x0409) ; English (USA)
^3::SetDefaultKeyboard(0x0411) ; Japanese
^4::SetDefaultKeyboard(0x0408) ; Greek

SetDefaultKeyboard(LocaleID){
	Static SPI_SETDEFAULTINPUTLANG := 0x005A, SPIF_SENDWININICHANGE := 2
	
	Lan := DllCall("LoadKeyboardLayout", "Str", Format("{:08x}", LocaleID), "Int", 0)
	VarSetCapacity(binaryLocaleID, 4, 0)
	NumPut(LocaleID, binaryLocaleID)
	DllCall("SystemParametersInfo", "UInt", SPI_SETDEFAULTINPUTLANG, "UInt", 0, "UPtr", &binaryLocaleID, "UInt", SPIF_SENDWININICHANGE)
	
	WinGet, windows, List
	Loop % windows {
		PostMessage 0x50, 0, % Lan, , % "ahk_id " windows%A_Index%
	}
}
script page: viewtopic.php?t=18519

The next process is to activate one language or another when one program or another is active. I imagine it can be done with a SetTimer that checks the active program every X time. It seems to complicate everything a lot when changing the language does not cost too much through the TrayIcon.
User avatar
yfjuu6
Posts: 125
Joined: 28 Apr 2023, 15:28

Re: Automatically change the keyboard layout language on specific windows ???

11 May 2023, 16:39

wetware05 wrote:
11 May 2023, 14:53
Hi, yfjuujy6

Step by step. Your script cannot be tested by someone else who does not have the two languages ​​you use in your script and the programs you reference.

Below is a script to switch between multiple languages, which references a function:

Code: Select all

^1::SetDefaultKeyboard(0x0406) ; Danish
^2::SetDefaultKeyboard(0x0409) ; English (USA)
^3::SetDefaultKeyboard(0x0411) ; Japanese
^4::SetDefaultKeyboard(0x0408) ; Greek

SetDefaultKeyboard(LocaleID){
	Static SPI_SETDEFAULTINPUTLANG := 0x005A, SPIF_SENDWININICHANGE := 2
	
	Lan := DllCall("LoadKeyboardLayout", "Str", Format("{:08x}", LocaleID), "Int", 0)
	VarSetCapacity(binaryLocaleID, 4, 0)
	NumPut(LocaleID, binaryLocaleID)
	DllCall("SystemParametersInfo", "UInt", SPI_SETDEFAULTINPUTLANG, "UInt", 0, "UPtr", &binaryLocaleID, "UInt", SPIF_SENDWININICHANGE)
	
	WinGet, windows, List
	Loop % windows {
		PostMessage 0x50, 0, % Lan, , % "ahk_id " windows%A_Index%
	}
}
script page: viewtopic.php?t=18519

The next process is to activate one language or another when one program or another is active. I imagine it can be done with a SetTimer that checks the active program every X time. It seems to complicate everything a lot when changing the language does not cost too much through the TrayIcon.

It worked, But I didn't know how to make it do the process automatically without pressing any key when specific window gets focus (I'm a newbie)
[Mod edit: Removed quote tags from non-quote. ]
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Automatically change the keyboard layout language on specific windows ???

11 May 2023, 17:32

It worked, But I didn't know how to make it do the process automatically without pressing any key when specific window gets focus (I'm a newbie)
[Mod edit: Removed quote tags from non-quote. ]
When do you want Arabic to be activated and when English? With what programs?
User avatar
yfjuu6
Posts: 125
Joined: 28 Apr 2023, 15:28

Re: Automatically change the keyboard layout language on specific windows ???

14 May 2023, 10:24

wetware05 wrote:
11 May 2023, 17:32
It worked, But I didn't know how to make it do the process automatically without pressing any key when specific window gets focus (I'm a newbie)
[Mod edit: Removed quote tags from non-quote. ]
When do you want Arabic to be activated and when English? With what programs?

When VS Code (ahk_exe Code.exe) is active.
[Mod edit: Removed quote tags from non-quote.]
Last edited by yfjuu6 on 14 May 2023, 10:41, edited 1 time in total.
gregster
Posts: 9087
Joined: 30 Sep 2013, 06:48

Re: Automatically change the keyboard layout language on specific windows ???

14 May 2023, 10:26

@yfjuujy6, please don't put misleading quote tags around your own responses.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Rohwedder, sachalamp and 108 guests