The keyboard layout indicator in Windows is a rather inconspicuous thing. The letters typed on the machine in the wrong layout are always frustrating. At least me. Therefore, I always wanted to have as much of an indicator of the current keyboard layout as possible. Well, at least one with which you do not need to glance around the screen.
The ideal option would be to attach a small indicator right next to the cursor in the input field, but this turned out to be a difficult task: some applications do not use the system cursor at all. A simple and fairly convenient solution to the problem was to change the color of the taskbar and the window title throughout the system. As it turned out, this can be done programmatically using the undocumented features of the Desktop Window Manager API ...
So, we want to change the color of the Windows theme (the color of the taskbar and window titles) in order to see the current keyboard layout as clearly as possible. We will change the color of the Aero theme, that is, only versions starting with Vista are supported.
First we start the monitor of the current keyboard layout. It installs a
Windows Shell Hook (WH_SHELL) in order to track the change in layouts. To be precise, it is not the change in the layout that is being tracked, but the change in the input language. Unfortunately, this solution is not suitable for people who use several different layouts for one (for example, Japanese) language. In the handler, we only respond to the HSHELL_LANGUAGE event and send our main application information about the new selected language with a simple Windows message.
')
A small technical feature is that the shell hook handler must be placed in the DLL, and not in the executable file. Therefore, the installation handler rendered in a small, written in C ++ library Hooker.
There are several important points here, firstly, concerning 32- and 64-bit versions of Windows, and secondly, UAC and “administrator mode”.
The fact is that in order to intercept the language change event in both 32-bit and 64-bit applications in the 64-bit version of Windows, you must separately install 32-bit and 64-bit handlers. Therefore, the Hooker library and its controlling HookerWatcher application are built for both architectures, and the main application runs both versions.
A separate problem is the "administrator mode". In fact, applications running on behalf of the administrator are completely separate from applications without elevated privileges. Therefore, in order for the highlighting of the current input language to work in this isolated environment, it is necessary to install shell hooks for both architectures again in administrator mode. I didn’t do that anymore: if necessary, you can simply launch a second copy of the entire application “as administrator”, and everything will work as it should.
In order to manage the color of the Windows theme, we will use the
Desktop Window Manager that has appeared in the Vista API. It is this library that controls the display of windows in the latest versions of Windows.
- DwmIsCompositionEnabled - check that the DWM is working;
- DwmGetColorizationParameters - we get the current colors of the Windows theme;
- DwmSetColorizationParameters - set the current colors of the Windows theme.
The color setting is determined by the WDM_COLORIZATION_PARAMS structure not described in the DWM documentation and
found in the German section of the MSDN forum. For simplicity in the application settings, we suggest the user to independently change the colors of the Windows theme and indicate that the current color should be used for one primary language or for all others. That is, in the application settings, we store only two instances of the WDM_COLORIZATION_PARAMS structure - for the “main” language and for all others.
The main application uses Windows Forms, consists of a single form with an overloaded WndProc method, in which the messages received from the monitor of the current language are processed. After receiving the notification about changing the language, the application sets the color of the Windows theme using the DwmSetColorizationParameters method.
By the way, in Windows 7 it was possible to observe a rather amusing behavior of the indicator of the current language in a quick search in the Start menu: after entering a character in the Russian layout, the language switched to English for a moment and back. And, accordingly, when you enter the next letter, the indicator blinked. And the built-in indicator of the layout reacted with a noticeable delay, and he did not have such a problem with blinking. In Windows 8, the built-in indicator works instantly.
The source can be found on GitHub
here , and the release version can be downloaded
here .