Find out when keyboard layout is changed

情到浓时终转凉″ 提交于 2019-11-30 02:38:02

问题


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

Currently I call:

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.


回答1:


There is a way ...

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.

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

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)




回答2:


It looks like the keyboard layout is stored here: HKEY_CURRENT_USER\Keyboard Layout\Preload

When I changed keyboard languages, the order of settings there changed.

So you could possibly monitor the registry entry. Here's one way:

http://www.codeproject.com/KB/system/registrymonitor.aspx



来源:https://stackoverflow.com/questions/8289492/find-out-when-keyboard-layout-is-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!