Switch between three keyboard languages

ankitg121 pts0 comments

Switch between three keyboard languages | mnaoumov.dev

Skip to content

Go back<br>Switch between three keyboard languages<br>17 Dec, 2023<br>Hi folks.

On the regular basis I use three keyboards: English, Russian and Ukrainian. I need them quite often and I need a quick way to switch between them. Default Alt + Shift and Win + Space are not good enough. Cyclical changes don’t work well sometimes and I have to click more and look in the notification error for the language label.

There are many tools for switching languages such as Punto Switcher but all that I tried work fine with two languages and don’t suggest anything nice for three language users. There are some advices how to use three languages but I didn’t find them practical.

My need is to be able to quickly switch to the desired language regardless of the current language selected.

I came up with the following idea:

Left Control to switch to English language

Right Control to switch to Russian language

Right Alt to switch to Ukrainian language

But also I need to keep the default behavior of those buttons and the shortcuts using those keys.

I would like to share the solution I came up with:

I built a script for AutoHotkey

#Requires AutoHotKey v2.0<br>#SingleInstance Force<br>#Warn All, MsgBox<br>#UseHook

; Based on https://www.autohotkey.com/boards/viewtopic.php?f=6&t=18519

setDefaultKeyboard(localeId) {<br>; https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfoa<br>static SPI_SETDEFAULTINPUTLANG := 0x005A

; https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfoa<br>static SPIF_SENDWININICHANGE := 2

; https://learn.microsoft.com/en-us/windows/win32/winmsg/wm-inputlangchangerequest<br>static WM_INPUTLANGCHANGEREQUEST := 0x0050

; https://learn.microsoft.com/en-us/windows/win32/winmsg/wm-inputlangchange<br>static WM_INPUTLANGCHANGE := 0x0051

; https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-loadkeyboardlayouta<br>static KLF_ACTIVATE := 0x00000001

; https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-loadkeyboardlayouta<br>pwszKLID := Format("{:08x}", localeId)<br>Flags := KLF_ACTIVATE<br>keyboardLayout := DllCall("LoadKeyboardLayout", "Str", pwszKLID, "Int", Flags)

; https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfoa<br>uiAction := SPI_SETDEFAULTINPUTLANG<br>uiParam := 0<br>pvParam := keyboardLayout<br>fWinIni := SPIF_SENDWININICHANGE<br>DllCall("SystemParametersInfo", "UInt", uiAction, "UInt", uiParam, "UPtr", pvParam, "UInt", fWinIni)

windowIds := WinGetList()

for windowId in windowIds {<br>try {<br>PostMessage(WM_INPUTLANGCHANGEREQUEST, 0, keyboardLayout, , "ahk_id" . windowId)<br>PostMessage(WM_INPUTLANGCHANGE, 0, keyboardLayout, , "ahk_id" . windowId)<br>} catch {<br>; Skip access denied windows

; https://stackoverflow.com/questions/14701095/how-to-get-keyboard-layout-name-from-a-keyboard-layout-identifier<br>; https://learn.microsoft.com/en-us/globalization/windows-keyboard-layouts

localeId_English_USA := 0x0409<br>localeId_Russian_Russia := 0x0419<br>localeId_Ukrainian_Ehnanced := 0x20422

~LControl: {<br>global isAltGr := false<br>Sleep 100<br>if (isAltGr) {<br>return

SetDefaultKeyboard(localeId_English_USA)

~RControl: SetDefaultKeyboard(localeId_Russian_Russia)

~RAlt: SetDefaultKeyboard(localeId_Ukrainian_Ehnanced)

~LControl & RAlt: {<br>global isAltGr := true<br>SetDefaultKeyboard(localeId_Ukrainian_Ehnanced)<br>Gotchas here:

I’ve added links to every WinAPI function and constant for better maintainability. Surprisingly most of WinAPI examples I see on the Internet are written very poorly.

Tilde prefix ~ (https://www.autohotkey.com/docs/v2/Hotkeys.htm)

When the hotkey fires, its key’s native function will not be blocked (hidden from the system).

Physical Right Alt button in some keyboard layouts (in my case, both Russian and Ukrainian) act as AltGr, which is an equivalent of LControl & RAlt. Therefore it requires special check to distinguish LControl & RAlt (as a physical button of Right Alt) from fair Left Control. As I figured out, it triggers LControl handler first and then LControl & RAlt, that’s why I had to add a global variable isAltGr and a small delay to handle this difference.

Stay tuned!

UPD : After some time, I found the selected shortcuts not so pleasant to use, then I realized, there is a key that I used only a few time in my life: Caps Lock, so I decided to utilize it. Now Caps Lock + 1 – English, Caps Lock + 2 – Russian, Caps Lock + 3 – Ukrainian. It’s a bit unusual to have Caps Lock as a modifier, so it took my muscle memory a bit of time to kick in, but now I am happily using it.

The changes in the script I put before

CapsLock & 1:: SetDefaultKeyboard(localeId_English_USA)<br>CapsLock & 2:: SetDefaultKeyboard(localeId_Russian_Russia)<br>CapsLock & 3:: SetDefaultKeyboard(localeId_Ukrainian_Ehnanced)<br>migrated-from-wordpress<br>autohotkey<br>windows<br>keyboard<br>productivity

Back To Top

Share this post on: Share this post via WhatsApp Share this post on...

https windows winuser switch keyboard setdefaultkeyboard

Related Articles