Browse Source

Allow playing notes using the computer keyboard

pull/141/head
Patrick Desaulniers 6 years ago
parent
commit
a27c8d81ca
1 changed files with 40 additions and 0 deletions
  1. +40
    -0
      examples/MidiKeyboard/MidiKeyboardExampleUI.cpp

+ 40
- 0
examples/MidiKeyboard/MidiKeyboardExampleUI.cpp View File

@@ -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.
*/


Loading…
Cancel
Save