Electronic – ‘keyboard from scratch’ registering key presses

avrckeyboardmicrocontroller

For my first hardware hack I'm taking a stab at a 'mechanical keyboard from scratch' (where scratch means individual components).

I plan to use a teensy++ board (16MHz) and was wondering about how to scan for key presses, so far I was thinking of using a grid (matrix) for the keys, every row is an out pin and every column is a in pin, the switches connect them (along with a diode for limiting the direction of current to lessen ghosting).

So there would be a scanning cycle, during which a column is set to high (or low, depending) and then every row is tested for this value. My question concerns whether or not this would be accurate enough, it means that the switch has to be closed at the exact moment that row and column are checked otherwise the key wont be registered at all. Is this how it is done or is there some much better approach?

There is also the issue of key bounce, so either a delay between checking the same key (could be a result of the scanning loop) and/or a counter where a certain key to be activated a number of times.

Best Answer

What you describe is basically the way the 8 bit micros used to do it, before keyboards all got dedicated microcontrollers (which just did the same thing anyway).

Human user keyboard input isn't very fast. If you scan the key matrix once every 20 msec, and make it a rule that you only count the key as being pressed if you see it pressed on two consecutive scans, you will eliminate key-bounce.