我正在用 C# 编写一个程序,该程序在后台运行,并允许用户使用热键在活动窗口中切换键盘布局。 (Windows 仅支持 CTRL+SHIFT 和 ALT+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)?
发布评论
评论(4)
我认为关键是让代码在您希望更改其键盘布局的线程的上下文中执行。 您需要在此处执行一些 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
如果您只是为自己编写一些东西,另一种可能可以接受的方法是:为每个布局定义一个单独的组合键(例如 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.