1 import javax.sound.midi.*;
2 import java.awt.event.*;
5 * Functional representation of a MIDI note, which contains two MIDI events, note on and note off.
7 * @author Einar Pehrson
10 public class MooKeyboard extends KeyAdapter {
12 private boolean[] isOn = new boolean[120];
13 private static final int startNote = 48;
14 private static final int[] keyToNote = new int[120];
17 * Plays the appropriate MIDI NoteOn event.
19 public void keyPressed(KeyEvent e) {
21 // Retrieves the notenumber from the conversion array.
22 int noteNumber = keyToNote[e.getKeyCode()];
23 // If note is not already on and the key is mapped to a note, sends the NoteOn event.
24 if (!isOn[noteNumber] && noteNumber > 0) Moosique.getActiveChannel().noteOn(noteNumber, 127);
25 isOn[noteNumber] = true;
26 } catch (ArrayIndexOutOfBoundsException x) {
32 * Plays the appropriate MIDI NoteOff event.
34 public void keyReleased(KeyEvent e) {
36 // Retrieves the notenumber from the conversion array.
37 int noteNumber = keyToNote[e.getKeyCode()];
38 // Sends the NoteOff event.
39 Moosique.getActiveChannel().noteOff(noteNumber);
40 isOn[noteNumber] = false;
41 } catch (ArrayIndexOutOfBoundsException x) {
46 /* Maps keycodes (array indices) to MIDI note numbers using the following layout:
48 * 2 3 5 6 7 9 0 => # # # # # # #
49 * q w e r t y u i o p => c d e f g a b c d e
50 * s d g h j l => # # # # # #
51 * z x c v b n m , . => c d e f g a b c d
54 keyToNote[90] = startNote;
55 keyToNote[83] = startNote + 1;
56 keyToNote[88] = startNote + 2;
57 keyToNote[68] = startNote + 3;
58 keyToNote[67] = startNote + 4;
59 keyToNote[86] = startNote + 5;
60 keyToNote[71] = startNote + 6;
61 keyToNote[66] = startNote + 7;
62 keyToNote[72] = startNote + 8;
63 keyToNote[78] = startNote + 9;
64 keyToNote[74] = startNote + 10;
65 keyToNote[77] = startNote + 11;
66 keyToNote[44] = startNote + 12;
67 keyToNote[76] = startNote + 13;
68 keyToNote[46] = startNote + 14;
69 keyToNote[81] = startNote + 12;
70 keyToNote[50] = startNote + 13;
71 keyToNote[87] = startNote + 14;
72 keyToNote[51] = startNote + 15;
73 keyToNote[69] = startNote + 16;
74 keyToNote[82] = startNote + 17;
75 keyToNote[53] = startNote + 18;
76 keyToNote[84] = startNote + 19;
77 keyToNote[54] = startNote + 20;
78 keyToNote[89] = startNote + 21;
79 keyToNote[55] = startNote + 22;
80 keyToNote[85] = startNote + 23;
81 keyToNote[73] = startNote + 24;
82 keyToNote[57] = startNote + 25;
83 keyToNote[79] = startNote + 26;
84 keyToNote[48] = startNote + 27;
85 keyToNote[80] = startNote + 28;