I'm looked through the tutorials on how to use key bindings, and I have used them before, but now the situation is different. I have a piano roll, the keys of which correspond to keyboard keys. I need to use the keyPressed and keyReleased methods to the program knows when to stop and start the piano note.
EDIT:
Here's the working code after getting the answer:
In the JLayeredPane constructor:
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
mapKeyboard(im, am);
and the method and classes for respective keyPressed/keyReleased actions:
public void mapKeyboard(InputMap im, ActionMap am)
{
int count = 0;
for(int j = 0; j<10; j++)
{
im.put(KeyStroke.getKeyStroke(KeyCodes[count], 0, false), "KeyDown" + count + "");
im.put(KeyStroke.getKeyStroke(KeyCodes[count], 0, true), "KeyUp" + count + "");
am.put("KeyDown" + count + "", new WhiteKeyDown(count, j));
am.put("KeyUp" + count + "", new WhiteKeyUp(count, j));
count++;
}
for(int j = 0; j<7; j++)
{
im.put(KeyStroke.getKeyStroke(KeyCodes[count], 0, false), "KeyDown" + count + "");
im.put(KeyStroke.getKeyStroke(KeyCodes[count], 0, true), "KeyUp" + count + "");
am.put("KeyDown" + count + "", new BlackKeyDown(count, j));
am.put("KeyUp" + count + "", new BlackKeyUp(count, j));
count++;
}
for(int j = 10; j<17; j++)
{
im.put(KeyStroke.getKeyStroke(KeyCodes[count], 0, false), "KeyDown" + count + "");
im.put(KeyStroke.getKeyStroke(KeyCodes[count], 0, true), "KeyUp" + count + "");
am.put("KeyDown" + count + "", new WhiteKeyDown(count, j));
am.put("KeyUp" + count + "", new WhiteKeyUp(count, j));
count++;
}
for(int j = 7; j<12; j++)
{
im.put(KeyStroke.getKeyStroke(KeyCodes[count], 0, false), "KeyDown" + count + "");
im.put(KeyStroke.getKeyStroke(KeyCodes[count], 0, true), "KeyUp" + count + "");
am.put("KeyDown" + count + "", new BlackKeyDown(count, j));
am.put("KeyUp" + count + "", new BlackKeyUp(count, j));
count++;
}
}
class WhiteKeyDown extends AbstractAction
{
int index;
public WhiteKeyDown(int i, int j)
{
super("KeyDown" + i + "");
index = j;
putValue(Action.NAME, "KeyDown" + i + "");
putValue(ACTION_COMMAND_KEY, "KeyDown" + i + "");
}
@Override
public void actionPerformed(ActionEvent ke) {
if(isWhiteDown[index] == false)
{
channel.noteOn (((WhiteKey) WhiteKeys[index]).getNote (), 127);
isWhiteDown[index] = true;
WhiteKeys[index].setBackground(Color.LIGHT_GRAY);
Key key = (Key) WhiteKeys[index];
CreateOnEvent(key);
}
}
}
class WhiteKeyUp extends AbstractAction
{
int index;
public WhiteKeyUp(int i, int j)
{
super("KeyUp" + i + "");
index = j;
putValue(Action.NAME, "KeyUp" + i + "");
putValue(ACTION_COMMAND_KEY, "KeyUp" + i + "");
}
@Override
public void actionPerformed(ActionEvent ke) {
if(isWhiteDown[index] == true)
{
channel.noteOff (((WhiteKey) WhiteKeys[index]).getNote (), 127);
isWhiteDown[index] = false;
WhiteKeys[index].setBackground(Color.WHITE);
Key key = (Key) WhiteKeys[index];
CreateOffEvent(key);
}
}
}
class BlackKeyDown extends AbstractAction
{
int index;
public BlackKeyDown(int i, int j)
{
super("KeyDown" + i + "");
index = j;
putValue(Action.NAME, "KeyDown" + i + "");
putValue(ACTION_COMMAND_KEY, "KeyDown" + i + "");
}
@Override
public void actionPerformed(ActionEvent ke) {
if(isBlackDown[index] == false)
{
channel.noteOn (((BlackKey) BlackKeys[index]).getNote (), 127);
isBlackDown[index] = true;
BlackKeys[index].setBackground(Color.DARK_GRAY);
Key key = (Key) BlackKeys[index];
CreateOnEvent(key);
}
}
}
class BlackKeyUp extends AbstractAction
{
int index;
public BlackKeyUp(int i, int j)
{
super("KeyUp" + i + "");
index = j;
putValue(Action.NAME, "KeyUp" + i + "");
putValue(ACTION_COMMAND_KEY, "KeyUp" + i + "");
}
@Override
public void actionPerformed(ActionEvent ke) {
if(isBlackDown[index] == true)
{
channel.noteOff (((BlackKey) BlackKeys[index]).getNote (), 127);
isBlackDown[index] = false;
BlackKeys[index].setBackground(Color.BLACK);
Key key = (Key) BlackKeys[index];
CreateOffEvent(key);
}
}
}