Alaye Dong

Alaye Dong`s xLog

github
bilibili
email

Windows sets Capslock to switch between Chinese and English input

The default method for switching between Chinese and English input in Windows is not very user-friendly; the Shift key is used for both case switching and input method switching, which can lead to accidental presses. In contrast, macOS's CapsLock solution—short press to switch between Chinese and English, long press to enable Caps Lock—is more intuitive and reduces misoperation.

This article introduces how to use AutoHotkey to achieve a similar CapsLock switching method for Chinese and English input as in macOS, making input more efficient.

Installing AutoHotkey#

First, download and install AutoHotkey:

Once installed, proceed with the settings for Windows and AutoHotkey.

Windows Settings#

In Windows, you need to adjust some input method settings to ensure that the AutoHotkey script works properly.

  1. Disable the Shift key for switching between Chinese and English in Microsoft Pinyin Input Method

    • SettingsTime & LanguageLanguage & RegionInputMicrosoft PinyinKeyboard
    • Uncheck "Shift to switch between Chinese and English"
  2. Add an English input method

    • SettingsTime & LanguageLanguage & RegionAdd a language
    • Select English (United States) or another required English keyboard

AutoHotkey Settings#

Creating an AutoHotkey Script#

  1. After installing AutoHotkey, right-click on the desktop → NewAutoHotkey Script
  2. Right-click the newly created script file and select Edit
  3. Replace the content with the following script:

Using CapsLock to Switch Input Methods#

#Requires AutoHotkey v2.0
#SingleInstance Force

; Keep the script running in the background
Persistent

CapsLock::
{
    if (KeyWait("CapsLock", "T0.5")) {
        ; Short press CapsLock to switch between Chinese and English
        Send "{Alt Down}{Shift Down}{Alt Up}{Shift Up}"
    } else {
        ; Long press CapsLock to enable Caps Lock
        SetCapsLockState !GetKeyState("CapsLock", "T")
    }
}

Locking the Chinese Input Method to Chinese Input Mode#

To ensure that the Chinese input method defaults to Chinese input mode, you can use the following script:

Source: gist.github.com

#Include %A_ScriptDir%

timeInterval := 500

InChs() {
  ime_status := DllCall("GetKeyboardLayout", "int", 0, "UInt")
  return (ime_status & 0xffff) = 0x804 ; LANGID(Chinese) = 0x804
}

SwitchImeState(id) {
  SendMessage(0x283,  ; WM_IME_CONTROL
              0x002,  ; wParam IMC_SETCONVERSIONMODE
              1025,   ; lParam (Chinese)
              ,       ; Control (Window)
              id)
}

DetectHiddenWindows True

SetTimer Mainloop, 1000

MainLoop() {
  try {
    hWnd := WinGetID("A")
    id := DllCall("imm32\ImmGetDefaultIMEWnd", "Uint", hWnd, "Uint")

    if (InChs()) {
      SwitchImeState(id)
    }
  }
}

Setting the Script to Run at Startup#

To have the script run automatically each time you start your computer, you can convert it to an .exe file and place it in the Windows startup directory.

Converting AutoHotkey Script to Executable File#

  1. In the AutoHotkey Dash, find Compile Open Ahk2Exe - convert .ahk to .exe

  2. Select your .ahk script and compile it to .exe

  3. Move the generated .exe file to:

    C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp
    

This way, the script will automatically run every time Windows starts, without needing to execute it manually.

Summary#

With AutoHotkey, we can achieve short pressing CapsLock to switch between Chinese and English, and long pressing to enable Caps Lock, thus improving the input experience in Windows. Coupled with the script to lock the Chinese input mode, it ensures that the Chinese input method defaults to Chinese input.

References#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.