Advertisement
Guest User

入力ロケール変更関数

a guest
Apr 1st, 2024
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Autohotkey 2.86 KB | Source Code | 0 0
  1. #Requires AutoHotkey v2.0.12 ; v2.0以上なら多分問題ないが念のため
  2.  
  3. F1:: change_input_locale(0x4110411) ; 日本語
  4. F2:: change_input_locale(0x4090409) ; 英語
  5. F3:: change_input_locale(0x40A0C0A) ; スペイン語
  6. F4:: get_active_HKL() ; アクティブウィンドウの現在の入力ロケール識別子(旧名: キーボードレイアウトハンドル)を取得しクリップボードに入れる
  7.  
  8. ; ウィンドウへのPostMessage テスト用
  9. F5:: PostMessage(0x0050, 0, 0x4110411, , "A")
  10. F6:: PostMessage(0x0050, 0, 0x4090409, , "A")
  11.  
  12. change_input_locale(HKL) {
  13.     static WM_INPUTLANGCHANGEREQUEST := 0x0050
  14.         , GUIThreadInfo := Buffer((A_PtrSize == 8) ? 72 : 48)
  15.  
  16.     NumPut("UInt", GUIThreadInfo.Size, GUIThreadInfo)
  17.     DllCall("GetGUIThreadInfo", "UInt", 0, "Ptr", GUIThreadInfo, "UInt")
  18.     if !hwnd := NumGet(GUIThreadInfo, (A_PtrSize == 8) ? 16 : 12, "Ptr")
  19.         hwnd := WinActive("A")
  20.  
  21.     PostMessage(WM_INPUTLANGCHANGEREQUEST, 0, HKL, , hwnd)
  22.  
  23.     ; 以下、ウィンドウへのWM_INPUTLANGCHANGEREQUESTで変更できなかった場合の対応
  24.     Sleep(50)
  25.     Id_thread := DllCall("GetWindowThreadProcessId", "Ptr", hwnd, "UInt", 0, "UInt")
  26.     HKL_current := DllCall("GetKeyboardLayout", "UInt", Id_thread, "UInt")
  27.     if HKL_current != HKL {
  28.         ; 秀丸ではエディタ部分にフォーカスがあるとメッセージを受け付けなかったので一旦適当な場所へフォーカスを移す
  29.         ; フォーカスがあると変更できなかったウィンドウは確認できた中では秀丸とDeepLとタスクバーのみ、その他のウィンドウではIMEへのメッセージで変更可能だった
  30.         ; したがって、フォーカスを移動させる処理は該当リスト方式にする方が良いかもしれない
  31.         if !h_focus := ControlGetFocus("A")
  32.             return ; 現在のフォーカスが取得できないウィンドウ(DeepLとタスクバーなど)の場合はフォーカスを戻せず微妙な挙動になるので何もしない
  33.         if h_top_control := DllCall("GetWindow", "Ptr", hwnd, "UInt", 5, "Ptr") ; アクティブウィンドウのトップコントロールのハンドルを取得
  34.             ControlFocus(h_top_control)
  35.  
  36.         h_IME := DllCall("imm32\ImmGetDefaultIMEWnd", "Ptr", hwnd, "Ptr")
  37.         PostMessage(WM_INPUTLANGCHANGEREQUEST, 0, HKL, , h_IME)
  38.  
  39.         Sleep(50)
  40.         if h_focus
  41.             ControlFocus(h_focus)
  42.     }
  43. }
  44.  
  45. get_active_HKL() {
  46.     GUIThreadInfo := Buffer((A_PtrSize == 8) ? 72 : 48)
  47.     NumPut("UInt", GUIThreadInfo.Size, GUIThreadInfo)
  48.     DllCall("GetGUIThreadInfo", "UInt", 0, "Ptr", GUIThreadInfo, "UInt")
  49.     if !hwnd := NumGet(GUIThreadInfo, (A_PtrSize == 8) ? 16 : 12, "Ptr")
  50.         hwnd := WinActive("A")
  51.     Id_thread := DllCall("GetWindowThreadProcessId", "Ptr", hwnd, "UInt", 0, "UInt")
  52.     HKL := Format("0x{:X}", DllCall("GetKeyboardLayout", "UInt", Id_thread, "UInt"))
  53.     ToolTip(A_Clipboard := HKL)
  54.     SetTimer(() => ToolTip(), 1500)
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement