Java:禁用重复的击键并同时获取两个不同的击键

时间:2018-10-11 12:00:34

标签: java awt midi keystroke keyboard-input

我正在设计用于教育目的的钢琴模拟软件。当获得用户输入时,我必须将键盘中的按键绑定到音高。有时按住某个键会产生重复的击键。我想确保按住一个键可以保持音符,而不是快速重复。此外,我想记录笔记保存的时间。最好的方法是什么?

这是我的代码:

public class PianoApplet extends Applet {

        @Override
    public void init() {
            HashSet<Character> set=new HashSet();
        final Midi midi;
        try {
            midi = new Midi();
        } catch (MidiUnavailableException e1) {
            e1.printStackTrace();
            return;
        }
        // this is a standard pattern for associating method calls with GUI events
        // the call to the constructor of KeyAdapter creates an object of an
        // anonymous subclass of KeyAdapter, whose keyPressed method is called
        // when a key is pressed in the GUI
        addKeyListener(new KeyAdapter() {
                        @Override
            public void keyPressed(KeyEvent e) {
                char key = (char) e.getKeyCode();
                                set.add(key);
                                //for mset.iterator();

                switch (key) {
                case '1':
                    midi.beginNote(new Pitch(0).toMidiFrequency());
                                    //midi.play(1, 1000, Midi.DEFAULT_INSTRUMENT);
                                    setBackground(Color.red);
                    return;
                case '2':
                    midi.beginNote(new Pitch(1).toMidiFrequency());
                    setBackground(Color.green);
                    return;
                case '3':
                    midi.beginNote(new Pitch(2).toMidiFrequency());
                    setBackground(Color.blue);
                    return;
                                case '4':
                    midi.beginNote(new Pitch(3).toMidiFrequency());
                    setBackground(Color.MAGENTA);
                    return;
                                case '5':
                    midi.beginNote(new Pitch(4).toMidiFrequency());
                    setBackground(Color.yellow);
                    return;
                                case '6':
                    midi.beginNote(new Pitch(5).toMidiFrequency());
                    setBackground(Color.cyan);
                    return;
                                case '7':
                    midi.beginNote(new Pitch(6).toMidiFrequency());
                    setBackground(Color.ORANGE);
                    return;
                                case '8':
                    midi.beginNote(new Pitch(7).toMidiFrequency());
                    setBackground(Color.GRAY);
                    return;
                                case '9':
                    midi.beginNote(new Pitch(8).toMidiFrequency());
                    setBackground(Color.pink);
                                return;
                                case '0':
                    midi.beginNote(new Pitch(9).toMidiFrequency());
                    setBackground(Color.red);
                                return;
                                case '-':
                    midi.beginNote(new Pitch(10).toMidiFrequency());
                    setBackground(Color.blue);
                                return;
                                case '=':
                    midi.beginNote(new Pitch(11).toMidiFrequency());
                    setBackground(Color.white);
                                return;
                                case 'I':
                                    System.out.println("GEdrtrfhftgh");
                                    midi.changeInstrument();



                }
            }
        });

        addKeyListener(new KeyAdapter() {
                        @Override
            public void keyReleased(KeyEvent e) {
                char key = (char) e.getKeyCode();
                switch (key) {
                case '1':
                                 midi.endNote(1000);
                    midi.endNote(new Pitch(0).toMidiFrequency());
                    return;
                case '2':
                    midi.endNote(new Pitch(1).toMidiFrequency());
                    return;
                case '3':
                    midi.endNote(new Pitch(2).toMidiFrequency());
                    return;
                                case '4':
                    midi.endNote(new Pitch(3).toMidiFrequency());
                    return;
                                case '5':
                    midi.endNote(new Pitch(4).toMidiFrequency());
                    return;
                                case '6':
                    midi.endNote(new Pitch(5).toMidiFrequency());
                    return;
                                case '7':
                    midi.endNote(new Pitch(6).toMidiFrequency());
                    return;
                                case '8':
                    midi.endNote(new Pitch(7).toMidiFrequency());
                    return;
                                case '9':
                    midi.endNote(new Pitch(8).toMidiFrequency());
                    return;
                                case '0':
                    midi.endNote(new Pitch(9).toMidiFrequency());
                    return;
                                case '-':
                    midi.endNote(new Pitch(10).toMidiFrequency());
                    return;
                                case '=':
                    midi.endNote(new Pitch(11).toMidiFrequency());
                    return;
                                case 'i':
                                    setBackground(Color.BLACK);
                                    Midi.DEFAULT_INSTRUMENT=Instrument.ACCORDION.next();
                                return;
                }
            }
        });
    }   

}

0 个答案:

没有答案