How do scanned interface inputs work?
The trick with keyboard matrices like this is the fact that outputs can (on most microcontrollers) be in three possible states - HIGH, LOW, and High Impedance (Hi-Z).

When an output is in Hi-Z mode it is effectively disconnected.

So by setting three of the outputs to Hi-Z and the fourth output to LOW, and having the four inputs pulled up through resistors, pressing a button connects that input to a LOW signal.

In effect the currently activated (driven LOW) output becomes the ground connection for a standard pulled-up button.

By cycling through which output is playing the role of the ground you can have any number of buttons attached to your inputs.

It is simplest to think of each of the outputs as a switch to ground. Work with just one input for now, and you can get an idea of how it works:







simulate this circuit &ndash; Schematic created using CircuitLab

In that example outputs 1, 3 and 4 are Hi-Z. Output 2 is LOW. You can now see that pressing buttons 1, 3 or 4 will do nothing since the switch (output) connecting them to ground is open. Pressing button 2 will pull the input LOW, registering a button press.

Now just multiply that circuit 4 times, one for each input, and there's your matrix.

You can also turn the whole thing the other way up - use a pull-down resistor and drive each input HIGH instead of LOW - that way the inputs will register a HIGH for active. It makes no real difference which way around it's done.  Pull-ups and driving outputs LOW is the most common way though, because MCUs often have built-in pullup resistors and the ability to select open drain outputs which switch between LOW and Hi-Z much easier.