public class MooKeyboard extends KeyAdapter {
- private boolean[] isOn = new boolean[120];
- private static int startNote = 48;
- private static final int[] keyToNote = new int[120];
+ private boolean[] isOn;
+ private static int startNote;
+ private static int[] keyToNote = new int[120];
+ /**
+ * Sets up the synthesizer emulation.
+ */
+ public MooKeyboard() {
+ isOn = new boolean[120];
+ }
+
+ static {
+ startNote = 48;
+ makeKeyboardMapping();
+ }
+
/**
* Plays the appropriate MIDI NoteOn event.
*/
public void keyPressed(KeyEvent e) {
- try {
- // Retrieves the notenumber from the conversion array.
- int noteNumber = keyToNote[e.getKeyCode()];
- // If note is not already on and the key is mapped to a note, sends the NoteOn event.
- if (!isOn[noteNumber] && noteNumber > 0) Moosique.getActiveChannel().noteOn(noteNumber, 127);
- isOn[noteNumber] = true;
- } catch (ArrayIndexOutOfBoundsException x) {
- return;
+ if (!e.isControlDown()) {
+ try {
+ // Retrieves the notenumber from the conversion array.
+ int noteNumber = keyToNote[e.getKeyCode()];
+ // If note is not already on and the key is mapped to a note, sends the NoteOn event.
+ if (!isOn[noteNumber] && noteNumber > 0) Moosique.getActiveChannel().noteOn(noteNumber, 127);
+ isOn[noteNumber] = true;
+ } catch (ArrayIndexOutOfBoundsException x) {
+ return;
+ }
}
}
* Plays the appropriate MIDI NoteOff event.
*/
public void keyReleased(KeyEvent e) {
- try {
- // Retrieves the notenumber from the conversion array.
- int noteNumber = keyToNote[e.getKeyCode()];
- // Sends the NoteOff event.
- Moosique.getActiveChannel().noteOff(noteNumber);
- isOn[noteNumber] = false;
- } catch (ArrayIndexOutOfBoundsException x) {
- return;
+ if (!e.isControlDown()) {
+ try {
+ // Retrieves the notenumber from the conversion array.
+ int noteNumber = keyToNote[e.getKeyCode()];
+ // Sends the NoteOff event.
+ Moosique.getActiveChannel().noteOff(noteNumber);
+ isOn[noteNumber] = false;
+ } catch (ArrayIndexOutOfBoundsException x) {
+ return;
+ }
}
}
*/
public static void setOctave(int n) {
startNote = n * 12;
+ makeKeyboardMapping();
+ }
+
+ /**
+ * Increases or decreases the octave of the lower part of the keyboard (default = 4)
+ * @param increase true for increase, false for decrease
+ */
+ public static void setRelativeOctave(boolean increase) {
+ if (increase) {
+ if (startNote == 108) return;
+ setOctave((startNote/12) + 1);
+ } else {
+ if (startNote == 0) return;
+ setOctave((startNote/12) - 1);
+ }
}
/** Maps keycodes (array indices) to MIDI note numbers using the following layout:
* s d g h j l => # # # # # #
* z x c v b n m , . => c d e f g a b c d
*/
- static {
+ private static void makeKeyboardMapping() {
keyToNote[KeyEvent.VK_Q] = startNote;
keyToNote[83] = startNote + 1;
keyToNote[88] = startNote + 2;
keyToNote[48] = startNote + 27;
keyToNote[80] = startNote + 28;
}
-}
\ No newline at end of file
+}