在Windows中我們可以用“Ctrl+Shift”鍵來調入或切換中文輸入法,但是這樣做每次都是使位於輸入法列表頂端的那個輸入法首先被調用。通常我們都要連續按好幾次“Ctrl+Shift”才能將習慣的輸入法調出。我編制了一段小程序,通過它可以把任意一個輸入法放在輸入法列表的頂端。
它的原理是:使用LoadKeyboardLayout函數可以改變輸入法的順序,只要在第一個參數中傳遞目標輸入法的KeyboardlayoutName,第二個參數用KLF_REORDER就可以了。
例如,aa = LoadKeyboardLayout(″00000409″, KLF_REORDER) 使英文變成第一。那怎樣獲得KeyboardlayoutName呢?因為使用GetKeyboardLayoutname可以返回當前輸入法的KeyboardlayoutName,所以我們可以先用GetKeyboardLayoutList 函數來取得所有輸入法,再用activateKeyboardlayout()函數設置當前輸入法,最後就可以得到它的KeyboardlayoutName了。具體步驟如下:
打開VB後選擇標准的EXE文檔,在Form1上添加一個Combobox和一個command控件,輸入以下程序。
′以下的API函數用於輸入法操作
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′如果用戶尚未選擇輸入法,顯示出錯信息 MsgBox ″請先選擇一個輸入法″ Exit Sub End If ′改變輸入法順序 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) ′取得目前的輸入法 NoOfKBDLayout = GetKeyboardLayoutList(25, hKB(0)) ′取得所有輸入法 ′ReDim layoutlist(NoOfKBDLayout) As String For i = 1 To NoOfKBDLayout If ImmIsIME(hKB(i - 1)) = 1 Then ′中文輸入法 BuffLen = 255 RetCount = ImmGetDescription(hKB(i - 1), Buff, BuffLen) RetStr = Left(Buff, RetCount) Combo1.AddItem RetStr Else RetStr = ″English (American)″ ′英文輸入法 Combo1.AddItem RetStr End If Next ActivateKeyboardLayout hCurKBDLayout, 0 ′恢復原來的輸入法 End Sub
運行後,在combobox中選擇目標輸入法,按下command即可。。