Want to keep learning?

This content is taken from the Raspberry Pi Foundation & National Centre for Computing Education's online course, Understanding Computer Systems. Join the course to learn more.

Skip to 0 minutes and 4 seconds What happens in your computer when you press a key on your keyboard? First, a switch beneath the key closes, and current flows into a small chip in a keyboard. Each key has a scan code number, which corresponds to its position on the keyboard.

Skip to 0 minutes and 23 seconds The keyboard transmits this number as binary data to the computer’s CPU. The CPU is running the operating system, which constantly checks for key presses. As soon as the OS detects a key press, it immediately reacts and figures out where the data needs to go. The OS knows what software was active when the key was pressed, and based on this information, creates an event. In computing, an event is an action recognised and handled by software. Events often originate from computers’ hardware. Your OS knows the layout for your keyboard. It matches the scan code to the key pressed and the associated letter, converting the scan code into a Unicode number. Your text editing software then captures the event from the operating system.

Skip to 1 minute and 19 seconds Based on the choice of font, the software converts the Unicode number into an image. The software creates a binary representation of the image and sends it back to the CPU. The CPU forwards the binary representation to the Graphics Processing Unit, or GPU. The GPU translates the binary representation into the image displayed on your monitor.

Skip to 1 minute and 46 seconds What about when keyboards have symbols in different locations? How might your operating system understand an Arabic keyboard instead of the Latin alphabet? Why not share your thoughts and join in the discussion in the comments section below.

Pressing a key

Last week, we talked about what happens when you press the power button: what components and systems work together to start up your computer, what slows it down, what speeds it up, and what else is inside your computer.

In this step, we’re going to look at what happens when you press a key on the keyboard.

The process of pressing a key in something like Microsoft Word and seeing it displayed on the screen is a good demonstration of what a computer system is: a set of devices that input, process, output, and store data. It illustrates how the hardware on a computer communicates with the software, and vice versa.

The sequence of events when you press a key is as follows.

The key is pressed

As the key is pressed, a switch closes and current flows into a small chip in the keyboard.

If you want to know more about how the physical switches work, have a look at this very detailed YouTube video by Computerphile:

This is an additional video, hosted on YouTube.

Each key on the keyboard has a scan code. The number is based on where the key is on the keyboard. This is useful when it comes to keyboards that have different layouts and/or symbols. This scan code number is sent into the computer as binary.

A number is transmitted to the CPU

The binary data is transmitted from the keyboard into your computer.

The route the data takes will depend on how your keyboard is connected to the computer. If it’s attached by a USB cable, it will go to the USB port. If the keyboard is connected by Bluetooth, it will go to the Bluetooth receiver.

The data then travels to the brains of the computer: the CPU.

The data is interpreted by the operating system

Remember, the CPU is running the operating system. The operating system is constantly checking for key presses. It may look like it’s sitting there doing nothing, but the operating system must react immediately to key presses, so it has to be ready for them all the time.

An event is created by the OS and captured by application software

The operating system will convert the scan code into an ASCII or Unicode character based on the key pressed. You typically set the keyboard layout for your computer in the operating system, so it is the operating system that will know what letter is represented by, say, scan code 1 in this language/keyboard layout.

The OS creates an event depending on the resulting character. In computing, an event is an action that is recognised and handled by the software. It often originates from the hardware.

The OS must now send the event to the appropriate application. Many different applications are running, and the key press must be handled by the right one. The OS knows what software was active when the key was pressed, and sends the event to that application.

The application software in this example is Microsoft Word. It captures the event from the operating system.

The application software displays the character on the screen

Microsoft Word converts the character into an image based on your font choices in Microsoft Word.

It sends this image, as a binary representation, back to the CPU, which sends it to the GPU. The GPU translates the binary code into an image and displays it on the monitor.

A lot happens when you press one key. The main devices we’re going to focus on in the next few steps are input and output devices. Can you work out which device is the input and which is the output in the scenario above? Can you work out what is hardware and what is software? Share your answers in the comments.

Share this video:

This video is from the free online course:

Understanding Computer Systems

Raspberry Pi Foundation