Virtual key Code and Keyboard scan Code in key replacement of ASCII.jp Keyboard

Virtual key Code and Keyboard scan Code in key replacement of ASCII.jp Keyboard

  • By huaweicomputers
  • 30/05/2022

Earlier in this series, I introduced the structure of the PC keyboard ("the Secret of the Keyboard in Windows10"). Let's explain the key replacement this time.

In PC AT, the position of the key is represented by such a number, and the resulting scan code is defined. The figure is referenced from "Windows Platform Design Notes Keyboard Scan Code Specification", which defines the specification of keyboard hardware for Windows. On the Windows logo key or intermediate key (apps key), mark it directly in the figure because there is no location number defined

The key replacement utility has types that use the registry and API, called Hook

The keyboard in Windows is handled by the device driver and therefore cannot be accessed directly from the application. Therefore, it is equipped with a function to make key replacement possible. One is the level of the keyboard driver, which works by defining the key to replace in the registry. The other is to use an API called "Hook" in Windows, which is handled at the stage before keyboard events are applied.

ASCII.jp キーボードのキー入れ替えにおける、仮想キーコードとキーボードスキャンコード

After the former is set in the registry, restart Windows, require the keyboard driver to read the registry, so it can not be replaced in real time, every application is impossible. By contrast, the method of using Hook is possible because it can override critical events with a user-created program.

At present, one of these is used by the common "key replacement" and "key mapping" applications on the Internet. Although there are few things that use device drivers, device drivers now require electronic signatures and the money they need to get, so they are almost invisible in free software.

PowerToys's keyboard manager uses the latter's Hook type

PowerToys's Keyboard Manager is the type that uses Hook and does not need to be restarted. In addition, it is replaced with the level of the virtual key that has been converted by the layout DLL. What this means, such as the Caps Lock key, is always the same at the level of the Windows scan code, but if you layout the DLL, it is the "English" key in the case of the JIS keyboard. The function of Caps Lock is "Shift+Caps Lock". The English key outputs the VK_OEM_ATTN (0xF0, also known as VK_240) code. The virtual key code is output only when the keyboard is Japanese.

A little complicated is that this button pressed the event to be notified, let go of things are not notified. When the same key and the Shift key are pressed at the same time, the VK_CAPITAL code (0x14) is output, where an event occurs when it is released. It is probably that the Japanese language IME pressed the "British number" key and switched to half-corner English input. This kind of move can only define Microsoft, so I'm afraid in the case of MS-IME (there is no possibility for other companies to consider IME).

Because of this slightly special behavior, key replacement in the form of Hook needs to be handled independently by grasping the layout of the keyboard (language) and the state of the keys pressed so far. Foreign-made key distribution applications can not handle Caps Lock keys well because of such a background.

Previous page 12 next page

What is the ASCII Club?