Read and control the Chinese input method of Windows with VB

zhaozj2021-02-17  51

In Windows, we can use the "Ctrl Shift" key to transfer or switch Chinese input methods, but do this time every time, the input method located at the top of the input List is first called. Usually we must continue to press several "Ctrl Shift" to transfer the entries of the habit. I have prepared a small program that allows any input method to the top of the input method list.

Its principle is: Use the loadkeyboardLayout function to change the order of the input method, as long as the KeyboardLayoutName of the target input method is passed in the first parameter, the second parameter can be used by KLF_REORDER.

For example, aa = loadingKeyboardLayout ("00000409", KLF_REORDER changes English first. How do you get KeyboardLayoutName? Because using getKeyboardLayoutName, you can return the keyboardLayoutName of the current input method, so we can use the getKeyboardLayoutList function to get all the input methods, and use the activateKeyboardLayout () function to set the current input method, and finally you can get its keyboardLayoutname. Specific steps are as follows:

Open VB After selecting a standard EXE document, add a ComboBox and a Command control on Form1, enter the following procedure.

'The following API function is used for input method operation

Private Declare Function GetKeyboardLayoutList lib "user32" _

(BYVAL NBUFF AS Long, LPLIST AS Long) AS Long

Private Declare Function Immgetdescription LIB "Imm32.dll" _

Alias ​​"Immgetdescriptiona" (Byval HKL As Long, _

BYVAL LPSZ AS STRING, BYVAL UBUFLEN As Long AS Long

Private Declare Function Immisime LIB "IMM32.DLL" (Byval HKL As Long) As Long

Private Declare Function ActivateKeyboardLayout LIB "User32" _

(Byval HKL AS Long, Byval Flags As Long) As long

Private Declare Function GetKeyboardLayout Lib "User32" (Byval DwLayout As Long) AS Long

Private Declare Function GetKeyboardLayoutname Lib "User32" Alias ​​_

"GetKeyboardLayoutnamea" (Byval Pwszklid As String) As long

Private Declare Function LoadKeyboardLayout Lib "User32" Alias ​​"LoadKeyboardLayouta" _

(Byval Pwszklid AS String, Byval Flags As Long) As long

Const KLF_REORDER = & H8

Private NOOFKBDLAYOUT AS Long, i as long, J AS Long

Private HKB (24) As Long, Bufflen As Long

Private buff as string

Private retstr as string

Private Retcount As Long

Private Kln As String

Private Sub Command1_Click () if Combo1.listIndex = -1 Then 'If the user has not selected the input method, display an error message

Msgbox "Please select an input method first"

EXIT SUB

END IF

'Change the order of input

Kln = String (8, 0)

ActivateKeyboardLayout HKB (Combo1.ListIndex), 0

Res = getKeyboardLayoutName (KLN)

Res = loadingKeyboardLayout (KLN, KLF_REORDER)

ActivateKeyboardLayout Hcurkbdlayout, 0

End Sub

Private sub flow_load ()

BUFF = String (255, 0)

Hcurkbdlayout = getKeyboardLayout (0) 'gets the current input method

NOOFKBDLAYOUT = GetKeyboardLayoutList (25, HKB (0)) 'gets all input method

'Redim LayoutList (NOOFKBDLAYOUT) AS STRING

For i = 1 to noofBDLAYOUT

IF Immisime (HKB (i - 1)) = 1 Then 'Chinese input method

Bufflen = 255

Retcount = Immgetdescription (HKB (i - 1), buff, bufflen

Retstr = Left (buff, retcount)

Combo1.addItem Retstr

Else

Retstr = "English (american)" English input method

Combo1.addItem Retstr

END IF

NEXT

ActivateKeyboardLayout Hcurkbdlayout, 0 'Restores the original input method

End Sub

After running, select the target input method in ComboBox, press the Command. .

转载请注明原文地址:https://www.9cbs.com/read-29296.html

New Post(0)