The keyboard uses a special, dedicated interface to talk to the PC. The basic design and operation of this interface is largely unchanged since the days of the old IBM PC/AT of the mid-1980s. Only in the last few years has the availability of the universal serial bus (USB) on newer systems created an alternative way of attaching a keyboard to the PC. The conventional keyboard interface is still used in almost all PCs, however, despite USB's growing popularity.
The traditional keyboard interface is in some ways similar to a "stripped-down version" of a regular serial (COM) port. Communication between the keyboard and the PC is accomplished over the lines in the keyboard cable, which connect the internal controller in the keyboard with a matching device on the motherboard, called the keyboard controller. This is really a misnomer of sorts, since it's arguable that the chip within the system doesn't control the keyboard; the chip within the keyboard does! It would probably be better if it were called the keyboard interface controller, actually.
Motherboards in older PCs, which were designed before the invention of integrated chipsets, used an Intel 8042 chip for their keyboard controller. This became the standard on virtually all PCs. Today, motherboards don't necessarily include a physical 8042 chip, but they emulate its functionality for the sake of compatibility. The keyboard controller is also responsible for other tasks within the PC; read more about it here.
All keyboards that use standard keyboard connectors to attach to the motherboard use the regular keyboard interface. This is true whether they use the larger 5-pin DIN connector, or the smaller, 6-pin mini-DIN. Communication over the interface is accomplished using two signaling lines, and is governed by a number of special rules and protocols, as described in the page on interface signaling. On modern PCs the communication is bi-directional, with the keyboard's internal controller and the motherboard's keyboard controller each able to send and receive commands over the interface.
Note: On the earliest IBM
PCs the keyboard could only send data, not receive it.
For its part, the keyboard controller on the motherboard communicates with the rest of the system using a special interrupt request line (IRQ), which has been used for the keyboard since the first PCs: IRQ #1. When a scan code is received from the keyboard, the keyboard controller places the data at I/O address 60h, and activates IRQ #1. (For more on these system resources, see this section of the site.) The system responds to the interrupt, looks at address 60h in memory, and interprets the code, sending the correct data to the current application.
And that, in a nutshell, is how the keyboard interface works.
Tip: You can see these
resources if you look up your keyboard's properties in the Device Manager in Windows.
An alternative to the regular keyboard interface is to connect a USB keyboard to your USB-enabled PC. The universal serial bus is not a keyboard interface at all, but a general-purpose, multi-functional serial interface for all sorts of devices, from keyboards and mice to printers, scanners and even storage devices. As such, I won't describe it in detail here, other than to point out the obvious: the USB interface is very different from the old dedicated keyboard interface, and they are not interchangeable.
It's also important to realize that while USB devices are generally very simple to set up and easy to use, keyboards in particular can cause complications when connected using USB. The reason is that USB support is generally provided by Windows drivers, and Windows isn't loaded until well into the boot process. The system BIOS however, looks for the keyboard early in the boot process, and it is also needed before Windows is running if, for example, you want to boot to DOS mode.