Your browser doesn't support JavaScript Dealing with Keyboard Input - Windows Programming

Dealing with Keyboard Input

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_KEYDOWNON_WM_KEYUP. The prototype message handler is –

afx_msg void OnMsgName (UINT nFlags, CPoint point)

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 OnMsgName (UINT nFlags, CPoint point)

where nChar holds the character code and nRepCnt and nFlags have the same meanings as keystroke messages.

Some additional keyboard messages

SYSKEY 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_KEYDOWNON_WM_KEYUPON_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 hardware and language-independent methods 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 –

afx_msg void OnMsgName (UINT nFlags, CPoint point)

Where vKey specifies one of 256 possible virtual-key codes.

If the function succeeds, the return value indicates whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. A return value of zero indicates that the current window does not have keyboard focus.

Example

The following code segment illustrates key-down and char-character message handling by displaying the output of the keystroke and character values when a key is pressed.

Download Code