Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Requires AutoHotkey v2.0.12 ; v2.0以上なら多分問題ないが念のため
- F1:: change_input_locale(0x4110411) ; 日本語
- F2:: change_input_locale(0x4090409) ; 英語
- F3:: change_input_locale(0x40A0C0A) ; スペイン語
- F4:: get_active_HKL() ; アクティブウィンドウの現在の入力ロケール識別子(旧名: キーボードレイアウトハンドル)を取得しクリップボードに入れる
- ; ウィンドウへのPostMessage テスト用
- F5:: PostMessage(0x0050, 0, 0x4110411, , "A")
- F6:: PostMessage(0x0050, 0, 0x4090409, , "A")
- change_input_locale(HKL) {
- static WM_INPUTLANGCHANGEREQUEST := 0x0050
- , GUIThreadInfo := Buffer((A_PtrSize == 8) ? 72 : 48)
- NumPut("UInt", GUIThreadInfo.Size, GUIThreadInfo)
- DllCall("GetGUIThreadInfo", "UInt", 0, "Ptr", GUIThreadInfo, "UInt")
- if !hwnd := NumGet(GUIThreadInfo, (A_PtrSize == 8) ? 16 : 12, "Ptr")
- hwnd := WinActive("A")
- PostMessage(WM_INPUTLANGCHANGEREQUEST, 0, HKL, , hwnd)
- ; 以下、ウィンドウへのWM_INPUTLANGCHANGEREQUESTで変更できなかった場合の対応
- Sleep(50)
- Id_thread := DllCall("GetWindowThreadProcessId", "Ptr", hwnd, "UInt", 0, "UInt")
- HKL_current := DllCall("GetKeyboardLayout", "UInt", Id_thread, "UInt")
- if HKL_current != HKL {
- ; 秀丸ではエディタ部分にフォーカスがあるとメッセージを受け付けなかったので一旦適当な場所へフォーカスを移す
- ; フォーカスがあると変更できなかったウィンドウは確認できた中では秀丸とDeepLとタスクバーのみ、その他のウィンドウではIMEへのメッセージで変更可能だった
- ; したがって、フォーカスを移動させる処理は該当リスト方式にする方が良いかもしれない
- if !h_focus := ControlGetFocus("A")
- return ; 現在のフォーカスが取得できないウィンドウ(DeepLとタスクバーなど)の場合はフォーカスを戻せず微妙な挙動になるので何もしない
- if h_top_control := DllCall("GetWindow", "Ptr", hwnd, "UInt", 5, "Ptr") ; アクティブウィンドウのトップコントロールのハンドルを取得
- ControlFocus(h_top_control)
- h_IME := DllCall("imm32\ImmGetDefaultIMEWnd", "Ptr", hwnd, "Ptr")
- PostMessage(WM_INPUTLANGCHANGEREQUEST, 0, HKL, , h_IME)
- Sleep(50)
- if h_focus
- ControlFocus(h_focus)
- }
- }
- get_active_HKL() {
- GUIThreadInfo := Buffer((A_PtrSize == 8) ? 72 : 48)
- NumPut("UInt", GUIThreadInfo.Size, GUIThreadInfo)
- DllCall("GetGUIThreadInfo", "UInt", 0, "Ptr", GUIThreadInfo, "UInt")
- if !hwnd := NumGet(GUIThreadInfo, (A_PtrSize == 8) ? 16 : 12, "Ptr")
- hwnd := WinActive("A")
- Id_thread := DllCall("GetWindowThreadProcessId", "Ptr", hwnd, "UInt", 0, "UInt")
- HKL := Format("0x{:X}", DllCall("GetKeyboardLayout", "UInt", Id_thread, "UInt"))
- ToolTip(A_Clipboard := HKL)
- SetTimer(() => ToolTip(), 1500)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement