Fixing up the button creation and keypad state algorithms to not throw invalid casts.