소스 검색

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.
Sam Atkins 2 년 전
부모
커밋
bdd9bc16de
2개의 변경된 파일29개의 추가작업 그리고 16개의 파일을 삭제
  1. 27 14
      Userland/Applications/Piano/MainWidget.cpp
  2. 2 2
      Userland/Applications/Piano/MainWidget.h

+ 27 - 14
Userland/Applications/Piano/MainWidget.cpp

@@ -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 (m_keys_pressed[event.key()])
-        return;
-    else
-        m_keys_pressed[event.key()] = true;
-
-    note_key_action(event.key(), DSP::Keyboard::Switch::On);
-    special_key_action(event.key());
+    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();
+    }
+
     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()

+ 2 - 2
Userland/Applications/Piano/MainWidget.h

@@ -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);
-    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_off_pressed_keys();
     void turn_on_pressed_keys();
     void turn_on_pressed_keys();