更改其他进程的键盘布局

发布于 2024-07-08 13:03:29 字数 322 浏览 15 评论 0 原文

我正在用 C# 编写一个程序,该程序在后台运行,并允许用户使用热键在活动窗口中切换键盘布局。 (Windows 仅支持 CTRL+SHIFTALT+SHIFT

我使用 RegisterHotKey 来捕获热键,并且运行良好。

问题是我找不到任何 API 来更改焦点窗口的键盘布局。

ActivateKeyboardLayout 和 LoadKeyboardLayout 只能更改调用线程的键盘布局。

有谁知道如何更改不同线程的键盘布局(就像语言栏那样)?

I'm writing a program in C# that runs in the background and allows users to use a hotkey to switch keyboard layouts in the active window. (Windows only supports CTRL+SHIFT and ALT+SHIFT)

I'm using RegisterHotKey to catch the hotkey, and it's working fine.

The problem is that I can't find any API to change the keyboard layout for the focused window.

ActivateKeyboardLayout and LoadKeyboardLayout can only change the keyboard layout for the calling thread.

Does anyone know how to change the keyboard layout for a different thread (the way the Language Bar does)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌伤浅笑 2024-07-15 13:03:29
PostMessage(handle, 
    WM_INPUTLANGCHANGEREQUEST, 
    0, 
    LoadKeyboardLayout( StrCopy(Layout,'00000419'), KLF_ACTIVATE)
);
PostMessage(handle, 
    WM_INPUTLANGCHANGEREQUEST, 
    0, 
    LoadKeyboardLayout( StrCopy(Layout,'00000419'), KLF_ACTIVATE)
);
栖迟 2024-07-15 13:03:29

我认为关键是让代码在您希望更改其键盘布局的线程的上下文中执行。 您需要在此处执行一些 win32 互操作并了解 DLL 注入以使代码在远程线程中执行。

键盘挂钩处理程序看起来是一个不错的选择在这里为你。

看一眼
http://www.codeproject.com/KB/threads/winspy.aspx

I think the trick is to get your code to execute in the context of the thread whose keyboard layout you wish to change. You'll need to do some win32 interop here and learn about DLL Injection to get your code to execute in the remote thread.

A keyboard hook handler looks like a good option for you here.

Take a look at
http://www.codeproject.com/KB/threads/winspy.aspx

云淡月浅 2024-07-15 13:03:29
  function ChangeRemoteWndKeyboardLayoutToRussian(
    const RemoteHandle: THandle): Boolean;
  var
    Dumme: DWORD;
    Layout: HKL;
  begin
    Layout := LoadKeyboardLayout('00000419', KLF_ACTIVATE);
    Result := SendMessageTimeOut(RemoteHandle, WM_INPUTLANGCHANGEREQUEST,
      0, Layout, SMTO_ABORTIFHUNG, 200, Dumme) <> 0;
    if Result then    
      Result := SendMessageTimeOut(RemoteHandle, WM_INPUTLANGCHANGEREQUEST,
        RUSSIAN_CHARSET, Layout, SMTO_ABORTIFHUNG, 200, Dumme) <> 0;
  end;
  function ChangeRemoteWndKeyboardLayoutToRussian(
    const RemoteHandle: THandle): Boolean;
  var
    Dumme: DWORD;
    Layout: HKL;
  begin
    Layout := LoadKeyboardLayout('00000419', KLF_ACTIVATE);
    Result := SendMessageTimeOut(RemoteHandle, WM_INPUTLANGCHANGEREQUEST,
      0, Layout, SMTO_ABORTIFHUNG, 200, Dumme) <> 0;
    if Result then    
      Result := SendMessageTimeOut(RemoteHandle, WM_INPUTLANGCHANGEREQUEST,
        RUSSIAN_CHARSET, Layout, SMTO_ABORTIFHUNG, 200, Dumme) <> 0;
  end;
私藏温柔 2024-07-15 13:03:29

如果您只是为自己编写一些东西,另一种可能可以接受的方法是:为每个布局定义一个单独的组合键(例如 Alt+Shift+1 等),并使用 SendInput 在它们之间切换。

当然,可以使用的情况是有限的。

Another way that may be acceptable if you are writing something just for yourself: define a separate key combination for every layout (such as Alt+Shift+1, etc), and use SendInput to switch between them.

The circumstances in which this is usable are limited of course.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文