Using VB to read and control the Chinese input method of Windows

Source: Internet
Author: User

In Windows we can use the "Ctrl+shift" key to call in or toggle the Chinese input method, but doing so each time is to make the input method at the top of the IME list first called. Usually we have to press several times in a row "ctrl+shift" to the habit of the input method. I developed a small program, through which you can put any input method at the top of the Input method list.

Its principle is: Use the Loadkeyboardlayout function can change the order of input method, as long as in the first parameter pass the target input method of Keyboardlayoutname, the second parameter with Klf_reorder on it.

For example, AA = Loadkeyboardlayout (″00000409″, Klf_reorder) makes English the first. So how do you get keyboardlayoutname? Because using Getkeyboardlayoutname can return the keyboardlayoutname of the current input method, we can use the Getkeyboardlayoutlist function to get all the input methods first. Then use the Activatekeyboardlayout () function to set the current input method, and finally you can get its keyboardlayoutname. The specific steps are as follows:

After opening VB to select the Standard EXE document, add a ComboBox and a command control on the FORM1, enter the following program.

The following API functions are 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 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 an input method, an error message is displayed
Msgbox″ Please select an input method first ″
Exit Sub
End If
Change input Order
KLN = String (8, 0)
Activatekeyboardlayout hkb (Combo1.listindex), 0
res = Getkeyboardlayoutname (KLN)
res = Loadkeyboardlayout (KLN, Klf_reorder)
Activatekeyboardlayout hcurkbdlayout, 0
End Sub
Private Sub Form_Load ()
Buff = String (255, 0)
Hcurkbdlayout = getkeyboardlayout (0) to obtain the current input method
Noofkbdlayout = getkeyboardlayoutlist (hkb (0)) to obtain all input methods
′redim layoutlist (noofkbdlayout) as String
For i = 1 to Noofkbdlayout
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′ Restore the original Input method
End Sub

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.