The keyboard is the primary input device on most computers. Every time you press a key on the keyboard, it sends an electrical signal through the cable to the keyboard controller on the motherboard. The signal is transmitted along the I/O bus to the processor. The processor determines which key it is and assigns it the proper code.
The keyboard always has the interrupt request level of one. This means it can interrupt the processor when any key is pressed. If the processor is in the middle of doing something and the user presses a key, the processor will stop what it was doing, and process the key. This is how, in some operating systems, you can press the escape key while an operation is occuring to stop that operation.