Windows provides device-independent keyboard support for applications by installing a keyboard device driver that is appropriate for the keyboard currently attached to the system. The keyboard device driver translates keyboard input into messages which it posts to the appropriate windows in your application.
Each key on a keyboard has a unique value called a scan code assigned to it. A scan code is a device-dependent identifier for a physical key on the keyboard. A keyboard generates two scan codes when the user types a key—one when the key is pressed and another when the key is released.
A scan code is interpreted by the keyboard device driver which maps the scan code to a Windows-defined device-independent value called a virtual-key code. A scan code identifies the purpose of the key rather than the key itself. After translating a scan code, the device driver creates a message that includes the scan code, the virtual-key code, and other information about the keystroke, then places the message in the system message queue. Windows removes the message from the system message queue and posts it to the message queue of the appropriate thread. Eventually, the thread's message loop removes the message and passes it to the appropriate window procedure for processing. The following diagram illustrates the Windows keyboard input model: