Going with Bruce's code is always a good bet.

But on this one, mister_e might have the right ticket for you.

Variable sized keypads ...

Matrix Keypad routine
http://www.picbasic.co.uk/forum/showthread.php?t=3250