I'm making a drum app which has buttons that serves as my drums. What I want is that the buttons will trigger upon key press so the user can play drums with the keyboard.
I'm using the keyTyped event of a button to execute a sound using a key. It works fine but first the focus needs to be on that specific button first as a result I can't trigger those seperate buttons upon key press because the focus is only in one button.
private void btnBassDrumKeyTyped(java.awt.event.KeyEvent evt) {
if(evt.getKeyChar() == KeyEvent.VK_V){
try{
music=new FileInputStream(new File(bassSound));
AudioStream audios=new AudioStream(music);
AudioPlayer.player.start(audios);
}
catch(Exception e){
JOptionPane.showMessageDialog(null,e.getLocalizedMessage());
}
}
}
The solutions I can think of are: Applying multiple focuses upon form load(if possible), use different way to trigger button event upon key press(not through KeyListener and KeyEvent).
Do you guys know how to implement these solutions or if you have your own solution can you teach me? Thanks guys! :)