This is a beautifully clear explanation of how a keyboard circuit can be designed. How to make a keyboard – the matrix | komar's techblog