Understand virtual key code, scan code and ASCII character code

xiaoxiao2021-03-06  94

Keywords:

Virtual key: Virtual-key, scan code: scan code, ASCII character code

During writing the MFC application, you need to extension on the original CEDIT function, and the newly generated class CEDITEX inherits to CEDIT, only allows the user to enter numbers and decimal points.

To implement only the user is allowed to enter the number and decimal points, you need to block the characters of non-numbers and decimal points, and the mask work is performed in the OnChar message function: After pressing the keyboard, the WM_CHAR message is dected, and enter the onchar message function, with :: isdigit Nchar) Verify the number, use nchar == '.' Verify the decimal point to meet the parent class onchar function Continue processing: Cedit :: OnChar (nchar, nREPCNT, NFLAG).

In addition, some buttons such as ESC, Tab keys, when they do not trigger the WM_CHAR message and enter the onchar message function, then need to use :: TranslateMessage function to translate in the PRETRANSLATEMESSAGE function, actually in this function Middleway

Virtual key code (Virtua- key)

That is, PMSG-> WPARAM recompile

ASCII character code, translated

When the ASCII character code is between 0-127, the character message WM_CHAR will be submitted to the message queue.

try it:

If the message MSG is immediately used after calling :: translateMessage function, the next message will be found to be WM_CHAR, and WPARAM has been translated into ASCII character code.

Understand 1.

OnChar function parameter variable nchar is the ASCII character code. After the test only triggers the WM_CHAR message only when the ASCII code corresponding to the button is between 0-127, and enter the onChar message function, for example: type up and down, left and right, will not go to OnChar Function.

Understanding 2.

If you understand the computer keyboard I / O, you should know that each key on the keyboard corresponds to a scan code, the scan code is developed by the OEM life, and the keyboard produced by different vendors will also be inconsistent with the same button. In order to get rid of the case where the system device is inconsistent, the scan code map is mapped to a unified virtual key code, such as the Enter key is defined as VK_RETURN, and its 16-enumeration value is 0x0d.

转载请注明原文地址:https://www.9cbs.com/read-96177.html

New Post(0)