1

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! :)

Andre
  • 101
  • 9
  • KeyBindings are what you need - https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html / http://stackoverflow.com/a/22741216/1306811 – Gorbles Mar 10 '16 at 15:01
  • Simple example that supports invoking an Action using the mouse or the keyboard: http://stackoverflow.com/questions/35906270/how-to-put-actionlistenerand-actioncommand-to-multiple-jbuttons/35906545#35906545 – camickr Mar 10 '16 at 15:36
  • how do I create an action that plays the sound using KeyBindings? Sorry I'm not that good yet in that particular area.I'm using Netbeans btw @camickr – Andre Mar 10 '16 at 17:26
  • You already said it works if the component has focus. So you just create an Action with the code you posted above. Then you add a key binding for the "V" key. The link I gave you shows how to create a generic Action and bind it to number 0 -9. The concept is the same. Then Action and the KeyStroke will be different. – camickr Mar 10 '16 at 18:14
  • [How to Use Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) – MadProgrammer Mar 10 '16 at 21:17

1 Answers1

0

Thank you everyone! @Gorbles, @camickr @MadProgrammer

Yes seems like key bindings' the exact answer.

bassDrumAction = new AbstractAction() {
         @Override
         public void actionPerformed(ActionEvent e) {
             try{
                    music=new FileInputStream(new File("C:\\Users\\AMMUYUTAN\\Documents\\Java Specialization Course\\CS182P\\AudioSync\\src\\sound\\KickSound.WAV"));
                    AudioStream audios=new AudioStream(music);
                    AudioPlayer.player.start(audios);
                    }
                    catch(Exception ex){
                        JOptionPane.showMessageDialog(null,ex.getLocalizedMessage());
                    }
            }
        };
InputMap inputMap = btnBassDrum.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
          inputMap.put(KeyStroke.getKeyStroke("B"), "hitBassDrum");
          btnBassDrum.getActionMap().put("hitBassDrum", bassDrumAction);
Andre
  • 101
  • 9