Improved keyboard frame detection logic