找出何时更改键盘布局

Find out when keyboard layout is changed(找出何时更改键盘布局)

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

问题描述

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

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)

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

本文标题为:找出何时更改键盘布局

基础教程推荐