szgislle

szgislle

生命不息,折腾不止!

Vim input method switching solution based on AutoHotKey for Windows platform

As we all know, there has always been a problem with switching between Chinese and English input methods when using Vim in a Chinese environment. The community has also come up with some solutions, such as the AutoHotKey script im-select plugin. Today, I will provide a solution based on screen color to implement the switching of input methods by pressing the ESC key. The following is a demonstration of the effect:

output.gif

Window Spy#

The version I currently have installed is AutoHotKey v2.0.2. Using its Window Spy feature, we can easily view information about various windows and mouse information. Today, the version we want to implement is based on judging the hexadecimal color at a specified coordinate on the screen to determine the current input method environment.

image.png

As shown in the above figure, because the screenshot cannot capture the mouse position, my mouse is placed below the "中" character in the figure. When switching to English, it shows a capital letter "A". The color of the lower part of the "中" character is not present at the same position on the screen. The icon indicating the input method status of the Windows system is relatively fixed, so it is feasible to judge the current input method environment based on this. When setting it yourself, you can use Window Spy to obtain this coordinate.

AutoHotKey Script#

Now that we have a solution, we can start implementing it. We will use the syntax of version 2 to complete it and map the less frequently used CapsLock key on the keyboard to the ESC key. The following is the complete script:

#Requires AutoHotkey v2.0

; Set the coordinate mode to screen
CoordMode "Pixel", "Screen"
CoordMode "Mouse", "Screen"

; Application group for Vim mode
GroupAdd  "VimMode", "ahk_exe WindowsTerminal.exe"
GroupAdd  "VimMode", "ahk_exe Code.exe"
GroupAdd  "VimMode", "ahk_exe Obsidian.exe"

; Exit Vim mode
VimEsc() {
  Send "{Esc}"
  if (PixelGetColor(1985, 1422) = "0x464646")
    Send "{Shift}"
}


#HotIf WinActive("ahk_group VimMode")
ESC::VimEsc()
CapsLock::VimEsc()
#HotIf

In the above code, add the applications that need to take effect to the VimMode group. If there are more applications that need to use Vim mode, use Window Spy to get their ahk_exe and add them to the group with GroupAdd "VimMode", "ahk_exe {name}". Modify the hexadecimal color and coordinates according to your needs. Also, modify the content after Send to the shortcut key for switching between Chinese and English input methods in your input method. Here, mine is the Shift key. After that, run the script to achieve the functionality of "automatically switching to English input method when pressing Esc or CapsLock".

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