Using autohotkey to realize the function of "using English punctuation in Chinese" of one key switching input method

Posted by websitesca on Wed, 17 Nov 2021 17:43:22 +0100

As a programmer, especially the old code farmer who can't do anything without vim, the "use English punctuation in Chinese" function of the input method is essential. It allows you to avoid many unnecessary Chinese and English switching operations when writing code. However, this function is a burden when writing documents. Chinese documents are full of English punctuation. I think companies with a little requirements are not allowed. Therefore, the operation of "using English punctuation in Chinese" becomes a little frequent.

Unfortunately, neither the classic Sogou input method nor Microsoft's own Microsoft input method has a shortcut key to realize the switching operation of this function. Finally, before writing a document, I always need to open the input method setting interface, find the position of "using English punctuation in Chinese files", and click the hateful switch to turn it off. When writing code, I have to run in the reverse direction to turn this function on again. Ah ~ ~, what an anti human design! After enduring this worthless redundant operation for several years, I finally couldn't stand this garbage operation one night and decided to shortcut this function.

Method 1:
The first solution is naturally to try to find the location of the configuration item corresponding to the function, and try to directly modify the configuration to realize the opening and closing of the function, then try to use autohotkey to realize automatic operation, and finally map it to a shortcut key to complete the goal.

After some exploration, it is found that the configuration of Sogou input method corresponds to the DefaultSymbol field in C:\Users\Administrator\AppData\LocalLow\SogouPY.users C: \ users \ administrator \ appdata \ locallow \ sogoupy. Users \ 00000001 \ env.ini.000001\env.ini. Unfortunately, the automatic update of input method configuration cannot be realized by manually modifying the DefaultSymbol value.

The Microsoft input method records the configuration with the registry of HKEY_ CURRENT_ In the user \ software \ Microsoft \ inputmethod \ settings \ CHS \ useenglishpunctionsinchineseinputmode item, the test found that editing the value with regedit.exe can realize real-time configuration update. However, if you use the regWrite function of autohotkey to operate this value, the real-time update effect of the configuration will not be realized. The final test result is that the Microsoft input method can complete real-time switching by displaying in the front end of autohotkey and importing reg file by calling regedit with cmd. The encapsulation function is similar to the following:

SwitchUseEnPunctuation() ; {{{1
{
    RegRead, val, HKEY_CURRENT_USER\SOFTWARE\Microsoft\InputMethod\Settings\CHS, UseEnglishPunctuationsInChineseInputMode
    if (val)
        Run %comSpec% /c "regedit -s d:\home\vimd\include\enp0.reg"
    else
        Run %comSpec% /c "regedit -s d:\home\vimd\include\enp1.reg"
}

The content of enp0.reg is:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\SOFTWARE\Microsoft\InputMethod\Settings\CHS]
"UseEnglishPunctuationsInChineseInputMode"=dword:00000001

The contents of enp1.reg are:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\SOFTWARE\Microsoft\InputMethod\Settings\CHS]
"UseEnglishPunctuationsInChineseInputMode"=dword:00000000

The wonderful thing about this method is that the cmd call must use the display front-end method call (the final result is that a black box flashes). If you use the hide parameter to try to hide the pop-up window during cmd execution, the real-time configuration update effect will be invalid. Because the black box flashes every time the method is switched (disgusting), and I prefer to use Sogou input method in daily life, I finally gave up the scheme.

Method 2:
After some thinking, I suddenly found that another more general and ingenious method can be used to realize this requirement. First of all, we know that both Sogou and Microsoft input methods have a shortcut key to switch between Chinese and English punctuation. By default, this shortcut key is basically Ctrl +. In fact, this shortcut key is also useful in daily use. For example, you have turned on the function of "using English punctuation in Chinese". If you occasionally need to input Chinese punctuation, you can use Ctrl +. To switch the input punctuation type to Chinese at one time. The problem with this shortcut key is that it has no persistence. When you switch the input method to the English state once and then switch back, it will always restore the input punctuation type to the English state (if the "use English punctuation in Chinese" function is enabled).
Since the problem is that there is no persistence, if you always decide whether to automatically enter an additional Ctrl +. Shortcut key operation can achieve persistence according to whether the configuration of "use English punctuation in Chinese" is enabled every time you switch to Chinese. In fact, it can be done. The implementation process is recorded as follows:

  1. In order to facilitate autohotkey to manage the input method, we need to add "English (USA)" in the language list:


2. Ensure that the input method in Chinese (simplified, Chinese) language is only Sogou input method:

3. Search input method configuration:
a) Keep the default status of Sogou input method as Chinese

b) "Use English punctuation in Chinese" is enabled by default:

c) Turn off the shift shortcut key inside Sogou input method to switch to English state to avoid accidental switching to English state of Sogou input method when switching to Chinese
d) Ensure that the default "Chinese English punctuation switching: Ctrl + period" is enabled:

3. Create an autohotkey script, add Chinese and English input method switching function encapsulation, and add shortcut key mapping. The contents are as follows:

; Global bad environment settings ; {{{1

; The following environment parameters must be set, Otherwise, the shortcut key setting is invalid
#UseHook On
Setkeydelay, -1

; It is used to record whether Chinese punctuation is used in the Chinese state of Sogou input method, Not used by default
ime_us_cn_point := 0

GetIME() ; {{{1
{ ; Gets the active ime language layout of the current window ID Interface, This interface is one of the few interfaces that can correctly query the input method language status
    return DllCall("GetKeyboardLayout", "UInt", DllCall("GetWindowThreadProcessId", "UInt", WinActive("A"), "UInt", 0), "UInt")
}

SwitchIME() ; {{{1
{
    global ime_us_cn_point
    if (GetIME() = 0x8040804) ; = 0x8040804 = chinese
        SendMessage, 0x50, 0, 0x00000409, , A
    else
    {
        SendMessage, 0x50, 0, 0x00000804, , A
        if (ime_us_cn_point)
        {
            sleep 10 ; Dormancy 10 ms To guarantee 100%Success rate
            Send ^.
        }
    }
}

SetIMEtoEn() ; {{{1
{ ; SendMessage It is faster and more stable than sending input method switching shortcut keys, 0x50=WM_INPUTLANGCHANGEREQUEST
    SendMessage, 0x50, 0, 0x00000409, , A
}

SetIMEtoCn() ; {{{1
{ ; SendMessage It is faster and more stable than sending input method switching shortcut keys, 0x50=WM_INPUTLANGCHANGEREQUEST
    global ime_us_cn_point
    SendMessage, 0x50, 0, 0x00000804, , A
    if (ime_us_cn_point)
    {
        sleep 10 ; Dormancy 10 ms To guarantee 100%Success rate
        Send ^.
    }
}

SwitchUseEnPoint() ; {{{1
{
    global ime_us_cn_point
    if (ime_us_cn_point)
        ime_us_cn_point = 0
    else
        ime_us_cn_point = 1
    ; msgbox % ime_us_cn_point
    Send ^.
}

Shift::SwitchIME() ; realization Shift Key switching Chinese and English input method
^.::SwitchUseEnPoint() ; realization Ctrl+. Toggle the "use English punctuation in Chinese" switch effect

So far, you have realized the function of using shift to switch between Chinese and English input methods and Ctrl +. To switch the effect of "use English punctuation in Chinese". Then you just need to start the autohotkey script automatically.

Topics: Linux Windows shell