Original Post
Hello, I was wondering if someone could assist me with the following problem I have created a GUI that represents a small keyboard with 4 keys. I am trying to map each noteButton with a certain key on the actual keyboard of the pc. For example ,if I press the Shift key on the keyboard,then it should correspond to noteButton60 on the GUI keyboard and play the sound. At the moment I can click each noteButton using the mouse but I want to be able to do the same, but by using the keys on my keyboard. I have implemented the KeyListener class,but I am not sure how to solve the problem I explained above using this class. I would appreciate if someone could give me a hand with this.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class SynthUI extends Frame implements ActionListener,KeyListener
{
private static final int FRAME_WIDTH = 680;
private static final int FRAME_HEIGHT = 400;
//Declare and create buttons for the keyboard
Button noteButton60 = new Button();
Button noteButton61 = new Button();
Button noteButton62 = new Button();
Button noteButton63 = new Button();
/**
* Constructor for objects of class SynthUI
*/
public SynthUI(SynthController synthController)
{
super("MIDI SynthController");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setResizable(false);
setLayout(null);
setLocation(20, 5);
titleLabel = new Label("MIDI SynthController 2005");
noteButton60.setBounds(40,130,40,230);
noteButton61.setBounds(60,130,35,140);
noteButton62.setBounds(80,130,40,230);
noteButton63.setBounds(100,130,35,140);
//Add the buttons to the GUI
add(noteButton60);
add(noteButton61);
add(noteButton62);
add(noteButton63);
noteButton60.addActionListener(this);
noteButton61.addActionListener(this);
noteButton62.addActionListener(this);
noteButton63.addActionListener(this);
//KeyListener
noteButton60.addKeyListener(this);
noteButton61.addKeyListener(this);
noteButton62.addKeyListener(this);
noteButton63.addKeyListener(this);
noteButton60.setFocusable(true) ;
noteButton61.setFocusable(true) ;
noteButton62.setFocusable(true) ;
noteButton63.setFocusable(true) ;
}
public void keyTyped(KeyEvent e) { }
public void keyPressed(KeyEvent e) { }
public void keyReleased(KeyEvent e) { }
}