From a27c8d81ca867a2040610147fa04e7fe6aecf606 Mon Sep 17 00:00:00 2001 From: Patrick Desaulniers Date: Tue, 30 Apr 2019 22:28:02 -0400 Subject: [PATCH] Allow playing notes using the computer keyboard --- .../MidiKeyboard/MidiKeyboardExampleUI.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/examples/MidiKeyboard/MidiKeyboardExampleUI.cpp b/examples/MidiKeyboard/MidiKeyboardExampleUI.cpp index 47a9cd1b..e17b58ab 100644 --- a/examples/MidiKeyboard/MidiKeyboardExampleUI.cpp +++ b/examples/MidiKeyboard/MidiKeyboardExampleUI.cpp @@ -40,6 +40,9 @@ public: // Add a min-size constraint to the window, to make sure that it can't become too small setGeometryConstraints(kUIWidth, kUIHeight, true, true); + + // Avoid key repeat when playing notes using the computer keyboard + getParentWindow().setIgnoringKeyRepeat(true); } protected: @@ -72,6 +75,43 @@ protected: glClear(GL_COLOR_BUFFER_BIT); } + /** + Allow playing notes using the bottom and top rows of the computer keyboard. + */ + bool onKeyboard(const KeyboardEvent& ev) override + { + // Offset from C4 + int offset = -1; + + const int keyjazzKeyCount = 26; + const uint keyjazzKeysQwerty[keyjazzKeyCount] = {'z', 's', 'x', 'd', 'c', 'v', 'g', 'b', 'h', 'n', 'j', 'm', ',', 'q', '2', 'w', '3', 'e', 'r', '5', 't', '6', 'y', '7', 'u', 'i'}; + + for (int i = 0; i < keyjazzKeyCount; ++i) + { + if (ev.key == keyjazzKeysQwerty[i]) + { + offset = i; + + // acknowledge duplicate C5 + if (i > 12) + { + offset -= 1; + } + + break; + } + } + + if (offset == -1) + { + return false; + } + + fKeyboardWidget.setKeyPressed(offset, ev.press, true); + + return true; + } + /** Called when a note is pressed on the piano. */