From bdd9bc16ded4c55e5e479b2b2357afc99ce65da8 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 14 Dec 2022 16:13:09 +0000 Subject: [PATCH] Piano: Only treat unmodified key presses as playing notes This makes Action shortcuts work again. :^) `note_key_action()` and `special_key_action()` now return whether they consumed the event. We don't even call them if any modifier keys were held down, so things like `Ctrl+T` no longer play notes. --- Userland/Applications/Piano/MainWidget.cpp | 39 ++++++++++++++-------- Userland/Applications/Piano/MainWidget.h | 4 +-- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/Userland/Applications/Piano/MainWidget.cpp b/Userland/Applications/Piano/MainWidget.cpp index e95ae70d017..be60e544d9d 100644 --- a/Userland/Applications/Piano/MainWidget.cpp +++ b/Userland/Applications/Piano/MainWidget.cpp @@ -79,14 +79,24 @@ void MainWidget::custom_event(Core::CustomEvent&) void MainWidget::keydown_event(GUI::KeyEvent& event) { - // This is to stop held-down keys from creating multiple events. - if (m_keys_pressed[event.key()]) - return; - else - m_keys_pressed[event.key()] = true; + if (!event.alt() && !event.ctrl() && !event.shift()) { + // This is to stop held-down keys from creating multiple events. + if (m_keys_pressed[event.key()]) + return; + else + m_keys_pressed[event.key()] = true; + + bool event_was_accepted = false; + if (note_key_action(event.key(), DSP::Keyboard::Switch::On)) + event_was_accepted = true; + if (special_key_action(event.key())) + event_was_accepted = true; + if (!event_was_accepted) + event.ignore(); + } else { + event.ignore(); + } - note_key_action(event.key(), DSP::Keyboard::Switch::On); - special_key_action(event.key()); m_keys_widget->update(); } @@ -98,27 +108,30 @@ void MainWidget::keyup_event(GUI::KeyEvent& event) m_keys_widget->update(); } -void MainWidget::note_key_action(int key_code, DSP::Keyboard::Switch switch_note) +bool MainWidget::note_key_action(int key_code, DSP::Keyboard::Switch switch_note) { auto key = m_keys_widget->key_code_to_key(key_code); if (key == -1) - return; + return false; m_track_manager.keyboard()->set_keyboard_note_in_active_octave(key, switch_note); + return true; } -void MainWidget::special_key_action(int key_code) +bool MainWidget::special_key_action(int key_code) { switch (key_code) { case Key_Z: set_octave_and_ensure_note_change(DSP::Keyboard::Direction::Down); - break; + return true; case Key_X: set_octave_and_ensure_note_change(DSP::Keyboard::Direction::Up); - break; + return true; case Key_Space: m_player_widget->toggle_paused(); - break; + return true; } + + return false; } void MainWidget::turn_off_pressed_keys() diff --git a/Userland/Applications/Piano/MainWidget.h b/Userland/Applications/Piano/MainWidget.h index dac1a739043..c72e26a6509 100644 --- a/Userland/Applications/Piano/MainWidget.h +++ b/Userland/Applications/Piano/MainWidget.h @@ -39,8 +39,8 @@ private: virtual void keyup_event(GUI::KeyEvent&) override; virtual void custom_event(Core::CustomEvent&) override; - void note_key_action(int key_code, DSP::Keyboard::Switch); - void special_key_action(int key_code); + bool note_key_action(int key_code, DSP::Keyboard::Switch); + bool special_key_action(int key_code); void turn_off_pressed_keys(); void turn_on_pressed_keys();