Default Keyboard Lang

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Default Keyboard Lang

31 May 2016, 20:11

The function sets the default input language, meaning it changes the input language of all subsequent threads and the input language of all non hidden windows.
The Locale identifiers can be found at Language Identifier Constants and Strings.
Script:

Code: Select all

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%
	}
}
Test:

Code: Select all

^1::SetDefaultKeyboard(0x0406) ; Danish
^2::SetDefaultKeyboard(0x0409) ; English (USA)
^3::SetDefaultKeyboard(0x0411) ; Japanese
^4::SetDefaultKeyboard(0x0408) ; Greek
"The specified language must be displayable using the current system character set." (SystemParametersInfo function)
Please excuse my spelling I am dyslexic.
Heihachi88
Posts: 29
Joined: 09 Aug 2018, 11:24

Re: Default Keyboard Lang

09 Aug 2018, 21:35

Hi, thanks for the script, i've noticed one issue with it it.

If i open Search panel (Win+s) then i can't change input language, i'm pressing Ctrl+1/2/3/4, but input is not changing, same with notification panel, when i pres Win+a, script is not working either. Possible to fix it?
YMP2
Posts: 48
Joined: 20 Apr 2014, 06:55

Re: Default Keyboard Lang

10 Aug 2018, 09:23

If you just need to change the input language of the active window, try this:

Code: Select all

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

SetInputLang(Lang)
{
    WinExist("A")
    ControlGetFocus, CtrlInFocus
    PostMessage, 0x50, 0, % Lang, %CtrlInFocus%
}
Heihachi88
Posts: 29
Joined: 09 Aug 2018, 11:24

Re: Default Keyboard Lang

10 Aug 2018, 20:27

Thanks, but you misunderstood me. AHK script hotkeys works everywhere for me, except these 2 places:

1. https://yadi.sk/i/uSXkPiuP3a87PF
2. https://yadi.sk/i/SAbTR-Yf3a87Qi
YMP2
Posts: 48
Joined: 20 Apr 2014, 06:55

Re: Default Keyboard Lang

10 Aug 2018, 22:29

No, I didn't misunderstand you. Have you tested my code with those two windows? For me, it works in them.
Heihachi88
Posts: 29
Joined: 09 Aug 2018, 11:24

Re: Default Keyboard Lang

11 Aug 2018, 00:43

Sorry, i just tried it now - it's indeed working even on the panels i mentioned above. Could you explain a bit, how it's different from Capn Odin's version?

Also is it possible to bind a single hotkey to toggle 2 input sources? I just have 2 languages, en and one more.
YMP2
Posts: 48
Joined: 20 Apr 2014, 06:55

Re: Default Keyboard Lang

11 Aug 2018, 01:07

My code sends the message to the control that has keyboard focus, rather than to its parent (main) window. Yes, you can toggle between languages.

Code: Select all

^1::ToggleInputLang()

ToggleInputLang()
{
    WinExist("A")
    ControlGetFocus, CtrlInFocus
    PostMessage, 0x50, 2,, %CtrlInFocus%
}
0x50 is WM_INPUTLANGCHANGEREQUEST and 2 is INPUTLANGCHANGE_FORWARD.
Heihachi88
Posts: 29
Joined: 09 Aug 2018, 11:24

Re: Default Keyboard Lang

11 Aug 2018, 01:20

Nice, thank you! If i add a 3rd language - it would just cycle them?
YMP2
Posts: 48
Joined: 20 Apr 2014, 06:55

Re: Default Keyboard Lang

11 Aug 2018, 01:43

Yes. There is also INPUTLANGCHANGE_BACKWARD (4), so you can assign it to a second hotkey in case you want to toggle between just two of the three.
imax
Posts: 1
Joined: 23 Nov 2019, 07:55

Re: Default Keyboard Lang

23 Nov 2019, 08:01

Please update to pass KLF_SUBSTITUTE_OK flag to LoadKeyboardLayout - it will allow the system to use a non-standard layout, if one is set by the user as preferred for a given language. https://docs.microsoft.com/en-ie/windows/win32/api/winuser/nf-winuser-loadkeyboardlayouta says that applications should always pass this flag.
timotv
Posts: 2
Joined: 18 Jan 2020, 01:49

Re: Default Keyboard Lang

19 Jan 2020, 06:58

Hello
Give me please a simple script for Autohotkey for switching between two languages. I just need to emulate Al+Shift combination to switch between RU and EN. I want to set this script for one of the additinal keys on keyboard.
timotv
Posts: 2
Joined: 18 Jan 2020, 01:49

Re: Default Keyboard Lang

22 Jan 2020, 10:12

Please check this scrip. Is it correct ?

Code: Select all

SendInput, {Alt down}{shift}
Send {Alt up}
Exit 
jlba
Posts: 1
Joined: 20 Jan 2021, 07:47

Re: Default Keyboard Lang

20 Jan 2021, 08:15

Hello,
I type in many different languages and I currently have a problem with the auto-correct function in word. I use the colemak layout when typing in a language which uses the Latin alphabet (seen by windows, and consequently by word as "English") and a custom "Russian phonetic colemak" when typing in Russian.
My current problem is the fact that when I switch from Russian to colemak, the spell checker automatically switch to "English" even if before that the document language was Swedish or Italian.
I have already found a solution: cloning my Colemak each time with a different layout native attribute (I use kbdedit for that). It works, the problem being, to switch languages, I often have to press 10 times in a row win + space, which is a bit annoying to say the least.
I tried this script and it seems it would solve my problem by allowing me to use a different shortcut for each languages.
My problem is, if I use for example

Code: Select all

^1::SetDefaultKeyboard(0x0419) ; Russian
to switch to Russian, it automatically add the standard Russian keyboard and switch to it (same for the other languages.
My customs keyboard have different KLID values (Standard Russian KLID = 00000419, my custom Russian KLID b0040419)
Based on the information found in this thread https://autohotkey.com/board/topic/18343-dllcall-loadkeyboardlayout-problem/ I tried to modify the script in the following way:

Code: Select all

SetDefaultKeyboard(LocaleID){
	Static SPI_SETDEFAULTINPUTLANG := 0x005A, SPIF_SENDWININICHANGE := 2
	
	Lan := DllCall("LoadKeyboardLayout", "Str", LocaleID, "UINT", 1)
	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%
	}
}

^1::SetDefaultKeyboard(a0000409) ; English
^2::SetDefaultKeyboard(b0030419) ; Russian
But it doesn't work as intended and when pressing ctr+1 it still load the default US keyboard (which is not active on my system).
Any idea what I do wrong?
Thank you in advance for any help :)

Lol, a few minutes after posting this message, I found what I had done wrong and managed to correct it. I do not remove my post, just in case someone would have a similar need.
The solution was

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance, Force;

SetDefaultKeyboard(LocaleID){
	Static SPI_SETDEFAULTINPUTLANG := 0x005A, SPIF_SENDWININICHANGE := 2
	
	Lan := DllCall("LoadKeyboardLayout", "Str", Format("{:08x}", LocaleID), "UINT", 1)
	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%
	}
}

^1::SetDefaultKeyboard(0xa0000409) ; English
^2::SetDefaultKeyboard(0xb0030419) ; Russian
A big thank you for this script which will go a long way in helping me preserving what remains of my sanity :)
User avatar
seven150528
Posts: 3
Joined: 04 Oct 2021, 23:18

Re: Default Keyboard Lang

22 Jan 2022, 11:34

if current keyboard layout is english then Topic is solved
viewtopic.php?t=19556

Thank you for everyting! Hats off you.
GameNtt
Posts: 154
Joined: 19 Aug 2022, 03:36

Re: Default Keyboard Lang

04 Jan 2023, 06:05

Three things. One, @jlba, was the error that 0x was not present?
Two, can anyone convert SetDefaultKeyboard function to v2?
Three, what is the difference between SetDefaultKeyboard and SetInputLang?

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 77 guests