What actually happens when a user types a character on a keyboard? What is the process before the character appears in a text editor such as Microsoft Word, for example?
Rationale: From an abstract point of view, operating systems are just pieces of software whose main goal is to hide hardware details from users. A good example is that when we type a character on a keyboard, it is automatically detected and processed for us by the operating system. Then, text editors or any other applications receive this input and, from this point, how the input is used is up to the software.
We will assume a standard keyboard, connected to the motherboard of the computer is used, along with any operating system.
First of all, it is good to know that all operating systems come with the so-called kernel. A kernel is a bit of functionality that manages resources provided by the hardware, such as the RAM memory, CPU and peripherals. Because this question is related to the use of a keyboard, we will focus on the kernel’s peripheral manager.
With most operating systems, each peripheral has a separate manager, often called a device driver. This is a piece of software that controls the peripheral and creates the link between the operating system and the keyboard, mouse, printer, or any other such device. The device driver is necessary because the OS needs to identify the peripheral correctly (i.e. know whether the device plugged in the motherboard is a mouse, a keyboard, a printer, etc.) and also know how to operate it properly (e.g. how to move information from the keyboard to the program that requires it). In Windows, the drivers of the various devices connected to the PC can be accessed via the Device Manager application.
This occurs mainly because some sort of control over peripherals is required. If programs were able to interact with the peripherals directly this could lead to hazardous situations (for example, if multiple programs try to access information from a peripheral at the same time). This is where the need of an intermediate entity arises (in this case, this entity is the operating system and its kernel).
So what are the actual steps when the user types a character, before it appears in a text editor or word processor?
- When a key is hit, an interrupt signal is transmitted from the keyboard. This signal is defined by a number.
- The keyboard device driver uses this number to execute the correct interrupt handler and process the input from the keyboard buffer.
- Note that if multiple keys are pressed at the same time, (or a key is pressed before the previous key is released), the interrupt handler is only executed after all the characters have been stored in the keyboard buffer.
- The interrupt handler lets the peripheral know it is ready for more data after the interrupt handler routine is executed.
- In the meantime, the information is passed on successively to the next functions of the kernel, until it reaches the application layer.
- At the application layer level, the information is picked up by the display driver.
- The display driver needs to know which application requested the information initially, so it performs a check on the current running applications.
- The text editor / word processor (e.g. Microsoft Word) tells the display driver this is the application the information needs to be directed to.
- The information is directed to the text editor / word processor.
Further discussions, such as using various types of keyboards (connection via PS/2 or USB, international keyboards) can continue with the interviewer at this point, based on to what extent the job is focused on the hardware part of computer science. Even if it’s not, it is always useful for a programmer to have a general idea about how peripherals work.