> the basics of how a computer works
This is a huge topic that’s not going to be answered in a single Reddit post.
I highly recommend [Ben Eater’s YouTube series](https://www.youtube.com/playlist?list=PLowKtXNTBypGqImE405J2565dvjafglHU), a bunch of videos where he builds a CPU from scratch. If you’re looking for a book, I’d recommend “But How Do It Know?” for something more ELI5-level, or if you’re into hands-on programming, [nand2tetris](https://www.nand2tetris.org/).
> What happens physically when the user interacts with the computer, when a button is pressed on the keyboard for example?
You have a computer (CPU) and a button, and when you press the button, the CPU “knows” that the button was pressed, and the program it’s running does something with that information.
What exactly happens depends on what kind of CPU, what kind of button, and how the system designer’s chosen to connect the button to the CPU.
The simplest possible setup is to have a button connected to one of the CPU pins. (Any chip will always have its delicate silicon parts factory-sealed inside a protective plastic case. By “CPU pins” I mean a bunch of literal pieces of metal sticking out of the case, for example [like this](https://en.wikipedia.org/wiki/File:Intel_80486DX2_bottom.jpg).)
The pins all look the same from the outside, but inside they’re connected to different parts of the CPU’s internal silicon circuits. If you read the manual published by the CPU manufacturer, you’ll find it needs to be powered by connecting two specific pins to a DC power supply, and some other pins can be (or in some cases, must be) connected to particular devices. But many of the remaining pins are “GPIO pins.” A GPIO pin is connected by default to a voltage sensing circuit. It’s a “digital” or “logic” sensing circuit, meaning it’s very simple and registers “off” for low voltages (0-2 V) or “on” for high voltages (like 3-5 V).
So the simplest possible setup is to have a button wired to the pin in such a way that it connects to the + terminal of the power supply when pressed, or the – terminal of the power supply when released. The CPU’s voltage sensing circuit then “knows” whenever the button’s pressed. When you’re writing the program running on the CPU, the GPIO pin usually appears as a “place in memory” that “changes on its own”. Meaning your program can check that place in memory, and do something when it changes.
There are a couple improvements that are typically done. First problem is the button is *incredibly slow* compared to the computer. It takes like 0.02 seconds for the button’s spring mechanism to move the contact from the – terminal to the + terminal. In that 0.02 second period the pin’s not connected to anything, and it can make the CPU’s internal voltage sensing circuit do some weird stuff.
The way to fix this is to re-wire the pin so it’s *always* connected to the + terminal — but not directly, you put a resistor with 10,000 ohms of resistance in the way. You then re-wire the button so that, when pressed, it connects the pin *directly* to the – terminal *in addition* to its existing connection to the + terminal. Since it’s a direct connection (copper wire has only like 0.2 ohms of resistance), the – terminal will “win” whenever the button’s pressed and the voltage sense circuit will read “off”.
This “pull up resistor” is so commonly needed that CPU designers usually put an internal resistor that your program can request it to connect. This means you don’t actually need to add the extra resistor to your circuit, if you just write your software to tell the CPU to use the internal one.
The next thing that needs fixed is your program is repeatedly asking “Is the button pressed?” (0.01 seconds later) “Is the button pressed?” (0.02 seconds later) “What about that button?” (0.03 seconds later) “Is it pressed *now*?”
It would be a lot more efficient and less annoying to program if you could just have your program tell the CPU once at the beginning while it’s setting up, “Hey, if the voltage sense circuit for pin #17 ever changes, please stop what I’m currently doing and run this subroutine instead.” And the CPU usually includes an “interrupt” mechanism that lets you set up exactly that kind of arrangement.
Now if you’re asking about how a PC keyboard works, well, it’s different and more complicated. A standard US PC keyboard has 104 keys. You can’t connect them all to individual pins: A CPU typically *doesn’t have* that many GPIO pins, and even if it did, to connect them all you’d need an impractically huge cable and a lot of complicated circuit board paths on the motherboard. So typically there’s a simple chip inside the keyboard that converts key-presses to coded pulses, that way you just need maybe 4 or 5 wires for power and signals, and only 2 or 3 of those wires need to be connected to CPU pins [1]. If you’re interested in the details [Ben Eater has a video about that](https://www.youtube.com/watch?v=7aXbh9VUB3U). (Actually several videos.)
[1] While my post and Ben Eater’s video are written as if the keyboard’s directly connected to CPU pins, I think in actual PC’s, keyboard pins for a PS/2 keyboard are probably not directly connected to the CPU. I’d assume they’d be connected to some separate chip on the motherboard that decodes the keypresses and sends them into the bus or something but I’m not 100% sure about that.
Latest Answers