【问题标题】:LoadKeyboardLayout() fails to change Keyboard Layout if the Windows Taskbar has current focus如果 Windows 任务栏具有当前焦点,则 LoadKeyboardLayout() 无法更改键盘布局
【发布时间】:2021-10-20 22:19:50
【问题描述】:

我正在使用全局热键以编程方式更改 Windows 中的键盘布局:

IntPtr fGWindow = GetForegroundWindow();
SendMessage(fGWindow.ToInt32(), WM_INPUTLANGCHANGEREQUEST_as_unit, IntPtr.Zero, LoadKeyboardLayout(LANG, KLF_SUBSTITUTE_OK));

代码运行良好,除非我使用鼠标单击 Windows 任务栏(为其提供当前焦点)。在这种情况下,消息已发送,但 LoadKeyboardLayout() 没有注意到。

使用HWND_BROADCAST 代替GetForegroundWindow() 没有帮助。

更新/更多信息:

当我点击任务栏(给它焦点)时GetForegroundWindow()获得的指针属于explorer.exe。在这种情况下,代码不起作用。

【问题讨论】:

    标签: c# wpf windows keyboard sendmessage


    【解决方案1】:

    这里是解决方法,如果当前前景窗口是系统托盘,基本上需要调整:

    IntPtr shell_TrayWnd = FindWindow("Shell_TrayWnd", null);
    
    if (fGWindow == shell_TrayWnd)
     {
       IntPtr vHandle = FindWindow("Progman", "Program Manager"); // Desktop Handler.
       SetForegroundWindow(vHandle);
       SendMessage(vHandle.ToInt32(), WM_INPUTLANGCHANGEREQUEST_as_unit, new IntPtr(-1), LoadKeyboardLayout(LANG, KLF_NOTELLSHELL));
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-02
      • 2012-09-04
      • 2012-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      • 1970-01-01
      相关资源
      最近更新 更多