Keyboard input is passed onto a program’s window procedure in the form of messages. Each time a key is pressed a message is sent to the window with input focus. Since most applications will have more than one window, a particular window must have input focus to receive these messages. 

Pressing keys on the keyboard will generate both a keystroke and a character. Keystrokes represent the physical keypress and characters represent the display symbol or glyphs generated as a result of the keypress.

When a key is pressed or released, a WM_KEYDOWN or WM_KEYUP message is placed in the message queue by Windows. These keystroke messages indicate the pressed key using a virtual key code. The virtual key code is a device-independent integer code that uniquely identifies a key on the keyboard. The corresponding MFC message map macros are ON_WM_KEYDOWN, ON_WM_KEYUP. The prototype message handler is-

afx_msg void OnMsgName (UINT nChar, UINT nRepCnt, UINT nFlags)

where
nChar – virtual key code of the key that was pressed or released.
nRepCnt – repeat count—the number of keystrokes.
nFlags – contains the key’s scan code.

In addition to producing keystrokes, character messages are also produced as a result of translating keystroke messages into character codes. The most commonly used character message is WM_CHAR. A WM_CHAR message includes a character code that maps directly to a symbol in the current character set. The ON_WM_CHAR macro entry in a class’s message map routes WM_CHAR messages to the member function OnChar(). The prototype is prototype is as follows:

afx_msg void OnChar (UINT nChar, UINT nRepCnt, UINT nFlags)

where nChar holds the character code and nRepCnt and nFlags have the same meanings that they have in keystroke messages.

Some additional keyboard messages

The WM_SYSKEYDOWN and WM_SYSKEYUP message is generated when the user presses the F10 key (menu bar) or holds down the ALT key and then presses another key. It also occurs when no window currently has the keyboard focus with the message being sent to the active window.

If other keys are pressed while the Alt key is held down Windows will generate a WM_SYSKEYDOWN and WM_SYSKEYUP messages instead of WM_KEYDOWN and WM_KEYUP messages.

The window that receives the message can distinguish between these two contexts by checking the context code in the lParam parameter.

The corresponding message-map macros are ON_WM_KEYDOWN, ON_WM_KEYUP, ON_WM_SYSKEYDOWN, and ON_WM_SYSKEYUP.

Handling WM_SYSKEYDOWN and WM_SYSKEYUP messages is generally best left to the system since if these messages don’t find their way to ::DefWindowProc and get returned to Windows then system keyboard commands such as Alt-Tab will stop working.

Dead keys

A dead key is a modifier key that does not generate a character but modifies the character generated by the key pressed immediately after it. Dead keys are typically used to attach a specific diacritic to a base letter.

To process dead-key messages in an MFC application will need an ON_WM_DEADCHAR or ON_WM_SYSDEADCHAR entry in the message map in addition to supplying handling functions named OnDeadChar() and OnSysDeadChar().

Virtual key codes

Windows defines special constants for each key the user can press. These constants, known as virtual key codes, provide a hardware and language-independent method of identifying keyboard keys. These values are listed in the link below

https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

Retrieving a key state

The ::GetKeyState() API function retrieves the status of a specified virtual key. The status specifies whether the key is up, down, or toggled. Since information about the current states of keys such as Shift and Ctrl keys is not included in keyboard messages, the GetKeyState API function allows the developer to determine these key states before deciding on a course of action. The syntax for this function is –

Display Code Download Code