Tuesday, April 8, 2014

What happens after a user presses a key on the keyboard?

Problem

Write a step by step execution of things that happen after a user presses a key on the keyboard. Use as much detail as possible.

Solution

Following are the steps
  1. You hear a click sound :P ;)
  2. The keyboard sends a scan code (electrical signal) of the key to the keyboard controller (Scan code for key pressed and key released is different).
  3. The keyboard controller interprets the scan code, i.e. if for instance 'A' is pressed,  and stores it in a buffer, until the process is ready to deal with it.
  4. The keyboard controller sends a hardware interrupt(kind of a signal) to the processor. This is done by putting signal on “interrupt request line”: IRQ 1. An interrupt tells the processor that some part of the computer has information for it to process and wants its attention. In this case, the keyboard controller wants the processor to look at the key you just pressed. An interrupt is a signal which tells the processor to stop what it was doing currently and do some special task.
  5. The processor is almost always doing many things, sharing its time among many tasks. As a result, most every event must wait its turn. The processor services interrupts based on their priority. When it is time to deal with the keypress, the processor routes it to the program for the operating system that you are using. 
  6. The interrupt controller maps IRQ 1 into INT 9.
  7. The processor invokes the “Interrupt handler”. CPU fetches the address of “Interrupt Service Routine” (ISR) from “Interrupt Vector Table” maintained by the OS (Processor use the IRQ number for this)
  8. The ISR reads the scan code from port 60h and decides whether to process it or pass the control to program for taking action. 
  9. UI rendering engine translates unicode codepoint to graphical image by loading respective font
  10. UI rendering engine translates unicode codepoint to graphical image by loading respective font

References

0 comments:

Post a Comment