A familiar linuksoid reproached me, they say, there is no Caps Lock language switch in Windows, nor can the layout be edited. I looked, and the truth is, all the layouts are contained in the files C: \ Windows \ System32 \ kbd * .dll, and I don’t call it a convenient editing hex editor.
How to achieve convenience? To switch the Caps Lock layouts, you can use all sorts of mounted programs, heavy ones like the Punto Switcher, or simple ones like lswitch. There is MSKLC for editing layouts, but it is low-functional and inconvenient, and analogues like KbdEdit or KLM32 are paid.
And then I decided to write on flat assembler the code that builds the layout DLL.
The basis was taken kbdusx.dll layout, in the system it is called "US International". I liked it because it has a number of combinations for additional characters. She didn’t like the fact that she turns the keys <`~>, <6 ^>, <'"> into the so-called dead keys, “dead keys.” Their feature is that when you press it, nothing is printed, but the next character typed changes. Thus You can enter Latin letters with diacritics, for which there is no separate shortcut key. But as a programmer, it was very unpleasant for me to stick to these frequently used keys, and initially I just nullified the fields in the hex editor that are responsible for dead keys.
It's time to deal with the format once and for all. At first, things went slowly, then I found kbd.h and a few examples of layouts from the Windows Driver Kit.
It's funny that Microsoft decided to put the layouts in the DLL format, although with the exception of the Korean kbdkor.dll and Japanese kbdjpn.dll there is no code at all, except for the exported function KbdLayerDescriptor, which returns a pointer to the main table with the layout.
The table has the following form:
32-bit Windows | 64-bit or WOW64 | |||
---|---|---|---|---|
Title | The size | Bias | The size | Bias |
modifiers | four | 0x00 | eight | 0x00 |
vk2wchar | four | 0x04 | eight | 0x08 |
deadkeys | four | 0x08 | eight | 0x10 |
keynames | four | 0x0C | eight | 0x18 |
keynamesExt | four | 0x10 | eight | 0x20 |
keynamesDead | four | 0x14 | eight | 0x28 |
scancode2vk | four | 0x18 | eight | 0x30 |
scancode2vk_size | one | 0x1C | one | 0x38 |
e0scancode2vk | four | 0x20 | eight | 0x40 |
e1scancode2vk | four | 0x24 | eight | 0x48 |
locale_flags | 2 | 0x28 | 2 | 0x50 |
version | 2 | 0x2A | 2 | 0x52 |
ligature_chars | one | 0x2C | one | 0x54 |
ligature_size | one | 0x2D | one | 0x55 |
ligatures | four | 0x30 | eight | 0x58 |
type | four | 0x34 | four | 0x60 |
subtype | four | 0x38 | four | 0x64 |
Thus, the layout file contains tables of the names of keys, conversion of scan codes into virtual (0x1C → VK_RETURN), conversion of virtual codes into symbols, combinations of dead keys, "ligatures".
Apparently, the names of the keys are rarely used, I could not find on my computer a program that would display their list from the layout.
By changing the table of scan codes, you can reassign any key to another. If you immediately wanted to reassign the power buttons (Power, Sleep, WakeUp), then this is also possible, but this will not cancel their original function. It can be disabled in the power settings.
In the symbol table, the most juice. It decides what the keystroke K, Shift + K, AltGr + K, Shift + AltGr + K will lead to, whether Caps Lock affects it, if it uses the same number of characters as Shift + K or if it does her Kana; will the character be typed immediately, will it be entered into a queue of dead keys, or will a series of characters from a “ligature” be printed, or will nothing happen.
AltGr - this is the name of the right Alt (if included in the layout with the KLLF_ALTGR flag), it generates the keyboard shortcut Ctrl + Alt.
The list of dead keys contains pairs of characters that must be converted to the third character. This symbol can be printed immediately, or go through the list again for further conversion. If a pair of characters is not found in the list, then it is simply printed as is. With the help of chains of dead keys, you can simulate the behavior of Compose Key, but some programs, for example, Firefox, do not recognize the transformation further than the first.
The ligatures list, which is essentially a set of macros, can allow you to type up to four WCHAR characters by pressing a key. In fact, I have up to 16 characters in Windows 7, but with an extremely unpleasant exception: Firefox freezes completely when stumbling on such a layout, and if the layout with long ligatures is system-wide, it stops running at all.
First, I wanted to make two convenient layouts, Russian and English, suitable for writing articles and programs. The typographical layout of Ilya Birman is good, but it could be even better, especially if you do it for yourself.
Then I found out about the Kana key. Kana - Caps Lock-like button on a Japanese keyboard. And then I decided to combine the English and Russian layouts into one, and switch between them with the Kana key. Which I reassigned to Caps Lock. I wrote a simple indicator program for displaying the state of Kana with a Caps Lock light.
This unified layout has one pleasant surprise: the layout is now one for the whole system, and remains when switching between programs, and also “very smart” programs can no longer switch the layout at will when editing text or moving the cursor.
And one unpleasant surprise: Psi + for some reason began to eat the first character entered after switching the layout by pressing Kana.
However, I left this layout in the archive along with the Kana indicator.
Then I discovered that Caps Lock can be assigned a separate row of characters and replaced Kana with Caps Lock. The indicator program has become unnecessary, an unpleasant surprise has disappeared. During alteration, only one restriction was found: Caps Lock only works with K and Shift + K rows - AltGr + K cannot be hung on it, etc.
In an attempt to fit all the dead keys in two rows (there were four with Kana), I came across the idea to reverse their order: so that the AltGr + letter was typed, and then the modifier. This allowed me to assign a lot more characters than before. I called it “undead keys”, “Dead Keys”.
And the perfect layout was ready.
Key combination | Result |
---|---|
AltGr + 5 | € |
AltGr + 9 | " |
AltGr + 0 | " |
AltGr + - | - |
AltGr + = | ≠ |
AltGr +; | ° |
AltGr +, | < |
AltGr +. | > |
AltGr + Shift + / | / me |
AltGr + a, ' | á |
AltGr + e, ` | è |
AltGr + o, " | ő |
AltGr + n, ~ | ñ |
AltGr + c, ^ | ĉ |
p, s, AltGr + q, ', b, a | a fish |
AltGr + c, o | © |
AltGr + r, o | ® |
AltGr + t, m | (tm) |
AltGr + m, u | µ |
AltGr + h, s | ☭ |
AltGr + f, 2 | ½ |
AltGr + b, f | █ |
I simply replace the system file C: \ Windows \ System32 \ kbdru.dll with my own layout, because it is better in all respects. On Windows XP or 2000, in this case, you need to remember to remove C: \ Windows \ System32 \ dllcache \ kbdru.dll.
But if you do not like such a forced upgrade of Windows, then the layout can be copied to the C: \ Windows \ System32 \ folder and registered in the system using the following reg-file:
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layouts\07430419] "Layout Text"="US+RU" "Layout Display Name"="United States-International + Russian + Extra" "Layout File"="kbdusru_undead.dll" "Layout Id"="00d0"
The first part of the code, 0743 is arbitrary numbers for unique identification of the layout, and 0419 is the code of the Russian language, under which the layout should appear. The behavior of the layout in non-unicode (ANSI) programs depends on the language code. If when typing letters appear question marks, then this is a true sign of an incorrectly specified language code.
It gives me great pleasure to program on flat assembler: a powerful macro language, a beautiful and docile syntax, the uselessness of any linkers and parameter porridge on the command line.
So I decided to write a program on it that reads the layouts DLL when assembling, and the source code at the output. Strange, right? Use assembler as disassembler. But the power of the fasm allows it.
As a result, I got layouts that allow switching between Caps Lock languages without external programs globally for the whole system, as well as a set of tools that allows us to conveniently edit the Windows keyboard layouts.
Everything is posted on https://github.com/grompe/kbdasm as public domain.
If you have 64-bit Windows, you can download the archive , unpack and run make.bat, and then install.bat. For a 32-bit one, you first have to tweak the kbdusru_undead.asm file.
Also in the archive there is a program get_scancodes. If you have a clever keyboard with additional keys, you can use the program to find out the scan codes and use these keys in the layout.
Source: https://habr.com/ru/post/301882/