我浏览了有关如何使用键绑定的教程,并且以前使用过它们,但是现在情况有所不同。我有一个钢琴卷,其琴键与键盘琴键相对应。我需要使用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

07-26 02:58