Improved keyboard handling