gpt4 book ai didi

java - Keybinders 可以实现 keyPressed 和 keyReleased 方法吗?

转载 作者:行者123 更新时间:2023-12-01 23:51:31 25 4
gpt4 key购买 nike

我浏览了有关如何使用键绑定(bind)的教程,并且我以前使用过它们,但现在情况不同了。我有一个钢琴卷轴,其琴键与键盘琴键相对应。我需要使用 keyPressed 和 keyReleased 方法让程序知道何时停止和开始钢琴音符。

编辑:

这是得到答案后的工作代码:

在 JLayeredPane 构造函数中:

     InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
mapKeyboard(im, am);

以及相应 keyPressed/keyReleased 操作的方法和类:

    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);
}
}
}

最佳答案

是的...

参见KeyStroke.getKeyStroke(int, int, boolean)

还有一个简单的exmple

关于java - Keybinders 可以实现 keyPressed 和 keyReleased 方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16247504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com