Switch between three keyboard languages
Recorded: May 30, 2026, 2:01 a.m.
| Original | Summarized |
Switch between three keyboard languages | mnaoumov.dev Left Control to switch to English language But also I need to keep the default behavior of those buttons and the shortcuts using those keys. ; Based on https://www.autohotkey.com/boards/viewtopic.php?f=6&t=18519 setDefaultKeyboard(localeId) { ; https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfoa ; https://learn.microsoft.com/en-us/windows/win32/winmsg/wm-inputlangchangerequest ; https://learn.microsoft.com/en-us/windows/win32/winmsg/wm-inputlangchange ; https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-loadkeyboardlayouta ; https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-loadkeyboardlayouta ; https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfoa windowIds := WinGetList() for windowId in windowIds { ; https://stackoverflow.com/questions/14701095/how-to-get-keyboard-layout-name-from-a-keyboard-layout-identifier localeId_English_USA := 0x0409 ~LControl: { SetDefaultKeyboard(localeId_English_USA) ~RControl: SetDefaultKeyboard(localeId_Russian_Russia) ~RAlt: SetDefaultKeyboard(localeId_Ukrainian_Ehnanced) ~LControl & RAlt: { 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. 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! |
The author addresses the persistent difficulty of switching between three keyboard languages—English, Russian, and Ukrainian—efficiently, noting that standard methods such as Alt Shift, Win Space, or cyclical changes are inadequate, and existing tools often only support two languages effectively. The core requirement is the ability to switch to the desired language rapidly, irrespective of the currently active setting. To solve this, the author proposes a custom key mapping based on the control and alt keys: Left Control for English, Right Control for Russian, and Right Alt for Ukrainian, while ensuring that the default functionality of these keys is maintained. The solution is implemented using an AutoHotkey script that interfaces with the Windows operating system via WinAPI calls to manage keyboard layout changes. The script details a function designed to set the system’s default input language by manipulating system parameters and loading keyboard layouts. This function handles the complex steps required to dynamically update the input language for active windows using messages such as WM_INPUTLANGCHANGEREQUEST and WM_INPUTLANGCHANGE. The script defines specific locale identifiers for English USA, Russian Russia, and Ukrainian Enhanced. A critical aspect of the implementation involves managing potential conflicts, particularly concerning physical key behavior. The author recognizes that the physical Right Alt key on certain layouts may function as AltGr, necessitating a mechanism to differentiate between a simple Left Control press and a combined Right Alt press (specifically, Left Control and Right Alt simultaneously) to accurately trigger the desired language switch. The script incorporates logic using a global variable to handle this distinction and introduces a slight delay to manage the sequence of events triggered by these modifier keys. Subsequently, the author revised the method of language switching for improved usability. Finding the custom hotkeys less intuitive, the author decided to utilize the Caps Lock key as a less frequently used modifier. The updated solution maps the language switches to combinations involving the Caps Lock key: Caps Lock plus one switches to English, Caps Lock plus two switches to Russian, and Caps Lock plus three switches to Ukrainian, providing an alternative, personalized mechanism for rapid language switching. |