The conventional console based programs (DOS & UNIX based) used to call the system calls such as read() to read a data from keyboard through some library. (e.g.): The device driver of keyboard stores the actual data (key pressed) in system buffer. A library (stdio) collects this data and passes to a program when a program calls read() system call. i.e a program reads the keyboard buffer when it is required. OS does not convey any message that a character has arrived from keyboard. The following flow explains this in simple form.
Keyboard driver : Keyboard —–> A key is pressed ——> data is stored in system bufferApplication Program : read() —–> if data is in keyboard buffer —–> data is read by application
In Windows OS, this scenario is different. OS makes call to application program. Let me put it in detail. (e.g.): Whenever a key is pressed in a keyboard, Windows (OS) store that event (e.g: Key Pressed or Released) and data in the system message queue. OS analyses that message and identifies the window belonging to that message. From this information, it can identify the process (thread) which created this window. OS then passes on this event to this thread’s message queue. This thread runs a loop processing the incoming messages. i.e OS conveys the message to an application. The following flow explains this in simple form. Note: An application thread may have more than one window. So a window does not mean an application, it means one of the windows of that application.
Keyboard driver : Keyboard —–> A key is pressed —-> this event and data are stored in system message queue (OS level)
Windows (OS) : Identifies window of the message —-> Identify the corresponding thread —-> Post the message to that thread’s message queue (Application level).
Application (thread) : Get the message —-> Translate the message (converting virtual key codes into actual characters) —–> Post back to thread’s message queue —-> Dispatches message to corresponding window procedure by calling that procedure.
Window Procedure : Identify the message type and act on that —-> complete and returns to application thread to retrieve the next message.
DispatchMessage() is a function implemented by the OS, which is called by the application thread. So the window procedure of an application is called by the OS!!! It is really strange from conventional programming.