无法将WM_INPUTLANGCHANGEREQUEST发送到某些控件

我正在(另一个) 键盘布局切换器,并与Skype窗口(Win7的64位版本6.22)奇怪的麻烦。 GetForegroundWindow()/ GetFocus()/ GetParentWindow()的任何组合都不能成功更改消息input内部的布局,更奇怪的是, 只有input了多个字符 。 其他情况下工作非常好,除了wpf应用程序拒绝服从没有focusedHandle的东西。

public static void SetNextKeyboardLayout() { IntPtr hWnd = GetForegroundWindow(); uint processId; uint activeThreadId = GetWindowThreadProcessId(hWnd, out processId); uint currentThreadId = GetCurrentThreadId(); AttachThreadInput(activeThreadId, currentThreadId, true); IntPtr focusedHandle = GetFocus(); AttachThreadInput(activeThreadId, currentThreadId, false); PostMessage(focusedHandle == IntPtr.Zero ? hWnd : focusedHandle, WM_INPUTLANGCHANGEREQUEST, INPUTLANGCHANGE_FORWARD, HKL_NEXT); } 

我是新来的winapi的东西,所以任何帮助将不胜感激,谢谢。

拆卸了一些工作产品后,我发现我接近正确的algorythm,看起来像这样:

 public static void SetNextKeyboardLayout() { IntPtr hWnd = IntPtr.Zero; var threadId = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero); var currentThreadId = GetCurrentThreadId(); var info = new GUITHREADINFO(); info.cbSize = Marshal.SizeOf(info); var success = GetGUIThreadInfo(threadId, ref info); // target = hwndCaret || hwndFocus || (AttachThreadInput + GetFocus) || hwndActive || GetForegroundWindow AttachThreadInput(threadId, currentThreadId, true); IntPtr focusedHandle = GetFocus(); AttachThreadInput(threadId, currentThreadId, false); if (success) { if (info.hwndCaret != IntPtr.Zero) { hWnd = info.hwndCaret; } else if (info.hwndFocus != IntPtr.Zero) { hWnd = info.hwndFocus; } else if (focusedHandle != IntPtr.Zero) { hWnd = focusedHandle; } else if (info.hwndActive != IntPtr.Zero) { hWnd = info.hwndActive; } } else { hWnd = focusedHandle; } if (hWnd == IntPtr.Zero) { hWnd = GetForegroundWindow(); } PostMessage(hWnd, WM_INPUTLANGCHANGEREQUEST, INPUTLANGCHANGE_FORWARD, HKL_NEXT); } 

但问题不在于找到PostMessage目标hWnd,而是在Skype的输入处理。 我已经通过在WM_INPUTLANGCHANGEREQUEST之前添加一个微小的延迟来解决它,所以Skype可以正确处理发送给它的所有输入。 现在我必须在没有这个延迟的情况下才能做事,但这是另一回事。

你应该试试这个: PostMessage(hWnd,WM_INPUTLANGCHANGEREQUEST,0,(LPARAM)HKL_NEXT);

PS :在Windows 10下,任何WM_ INPUTLANGCHANGEREQUEST Skype崩溃。

最好的方式与Windows 10 – 是模拟开关键盘布局的键,如下所示:

 keybd_event(VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY, 0); keybd_event(VK_SPACE,0, KEYEVENTF_EXTENDEDKEY, 0); Sleep(10); keybd_event(VK_SPACE,0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); keybd_event(VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);