1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:07:35 +00:00

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.
This commit is contained in:
Sam Atkins 2022-12-14 16:13:09 +00:00 committed by Linus Groh
parent 7c17e73c7f
commit bdd9bc16de
2 changed files with 28 additions and 15 deletions

View file

@ -79,14 +79,24 @@ void MainWidget::custom_event(Core::CustomEvent&)
void MainWidget::keydown_event(GUI::KeyEvent& event) void MainWidget::keydown_event(GUI::KeyEvent& event)
{ {
// This is to stop held-down keys from creating multiple events. if (!event.alt() && !event.ctrl() && !event.shift()) {
if (m_keys_pressed[event.key()]) // This is to stop held-down keys from creating multiple events.
return; if (m_keys_pressed[event.key()])
else return;
m_keys_pressed[event.key()] = true; 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(); m_keys_widget->update();
} }
@ -98,27 +108,30 @@ void MainWidget::keyup_event(GUI::KeyEvent& event)
m_keys_widget->update(); 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); auto key = m_keys_widget->key_code_to_key(key_code);
if (key == -1) if (key == -1)
return; return false;
m_track_manager.keyboard()->set_keyboard_note_in_active_octave(key, switch_note); 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) { switch (key_code) {
case Key_Z: case Key_Z:
set_octave_and_ensure_note_change(DSP::Keyboard::Direction::Down); set_octave_and_ensure_note_change(DSP::Keyboard::Direction::Down);
break; return true;
case Key_X: case Key_X:
set_octave_and_ensure_note_change(DSP::Keyboard::Direction::Up); set_octave_and_ensure_note_change(DSP::Keyboard::Direction::Up);
break; return true;
case Key_Space: case Key_Space:
m_player_widget->toggle_paused(); m_player_widget->toggle_paused();
break; return true;
} }
return false;
} }
void MainWidget::turn_off_pressed_keys() void MainWidget::turn_off_pressed_keys()

View file

@ -39,8 +39,8 @@ private:
virtual void keyup_event(GUI::KeyEvent&) override; virtual void keyup_event(GUI::KeyEvent&) override;
virtual void custom_event(Core::CustomEvent&) override; virtual void custom_event(Core::CustomEvent&) override;
void note_key_action(int key_code, DSP::Keyboard::Switch); bool note_key_action(int key_code, DSP::Keyboard::Switch);
void special_key_action(int key_code); bool special_key_action(int key_code);
void turn_off_pressed_keys(); void turn_off_pressed_keys();
void turn_on_pressed_keys(); void turn_on_pressed_keys();