Keystroke Messages

Keystrokes and Characters

When a key is pressed on a keyboard, an application may receive several key stroke messages as a result. The messages that may be generated when pressing or releasing a key are shown in the table below.

Pressed Released Key Type
message::key_down message::key_up Non-system key stroke
message::system_key_down message::system_key_up System key stroke

Usually, the 'down' and 'up' forms of the message occur in pairs; however, if a key is held down so that a typematic repeat occurs, a series of key down messages is sent with a single key up message to follow. All queued input messages have a time stamp, so that the function get_message_time may be used to obtain the relative time of a keystroke message.

System and Non-System Messages

The system keystroke messages mentioned in the previous section are primarly used by the operating system. They are generated when a key is pressed with the alt key depressed. These keystrokes involve options in a menu or system menu or they are used for system functions such as switching the active window (alt-tab and alt-esc). Generally, applications ignore these keystrokes, resulting in them being passed to the default window procedure. The default window procedure acts upon such keystrokes and notifies the application of resultant events using other messages (such as message::command for a menu selection). If system keystroke messages are intercepted, it is customary for them to be passed to the default window procedure after the application has finished processing them.

Shift State For Virtual Keys

The messages:

do not report the state of the shift keys. The current state of any virtual key may be obtained via the function get_key_state. This function is generally used to obtain the state of the shift keys (Shift, Ctrl and Alt) as well as the toggle keys (caps lock, num lock and scroll lock). For example, the call


returns a negative value (high bit set) when the shift key is down. The call


has the low bit set if the key is toggled on. Note that the function get_key_state returns the status as at the last message being received and it does not check the keyboard state as it presently is. The function that does that is get_asynchronous_key_state.