找出何时更改键盘布局

5

本文介绍了找出何时更改键盘布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在编写一个屏幕键盘,并希望在键盘布局更改后立即重新绘制我的布局.

I am writing an onscreen keyboard and would like to redraw my layout as soon as keyboard layout is changed.

目前我打电话:

GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow(), NULL));

在每次按键时查看布局是否已更改.如果用户通过鼠标更改布局,则它不起作用,直到按下键.

on every key press to find out if the layout has changed. It does not work if user changes the layout by mouse, until key is pressed.

我想知道是否有任何方法可以得到通知当当前前台窗口的键盘布局改变时,这样我就可以在更改发生后立即重新绘制布局.

I would like to know if there is any way to get notified when the keyboard layout of the current foreground window is changed, so I can redraw my layout as soon as the change happens.

推荐答案

有办法...

首先您需要注册您的应用程序以捕获前台窗口更改:
使用 SetWinEventHookEVENT_SYSTEM_FOREGROUND (和 WINEVENT_OUTOFCONTEXT,因为它是 .NET).

First you need to register your application to capture foreground window changes:
Use SetWinEventHook with EVENT_SYSTEM_FOREGROUND (and WINEVENT_OUTOFCONTEXT as it's .NET) for that.

如果发生这种情况:使用您的 GetKeyboardLayout 解决方案获取该窗口的当前布局.

If that happens: Use your GetKeyboardLayout solution for getting the current layout of that window.

然后使用本地 Windows Hook (您可能在低级别全局使用它来捕获密钥)WH_CALLWNDPROC 和新前台窗口的线程.
收听 WM_INPUTLANGCHANGE 消息接收布局更改的窗口.
(您可能希望在另一个前台更改后取消挂钩/重新挂钩)

Then use a local Windows Hook (you're probably using it low-level-globally for key captures) with WH_CALLWNDPROC and the thread of the new foreground window.
Listen to WM_INPUTLANGCHANGE messages to that window to receive changes to the layout.
(You may want to unhook/rehook after another foreground change)

这篇关于找出何时更改键盘布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14