From af72b5ec823f87f9e30d786d8b9ceb0d189885b1 Mon Sep 17 00:00:00 2001 From: Matthew Jones Date: Wed, 2 Jun 2021 15:59:08 -0600 Subject: [PATCH] LibGUI: Show pressed state for Space and Return key events Also allows the user to press Esc while the button is being pressed to cancel the button action. --- Userland/Libraries/LibGUI/AbstractButton.cpp | 18 +++++++++++++++++- Userland/Libraries/LibGUI/AbstractButton.h | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibGUI/AbstractButton.cpp b/Userland/Libraries/LibGUI/AbstractButton.cpp index f3f4e53df4..9c49dfc593 100644 --- a/Userland/Libraries/LibGUI/AbstractButton.cpp +++ b/Userland/Libraries/LibGUI/AbstractButton.cpp @@ -146,13 +146,29 @@ void AbstractButton::leave_event(Core::Event&) void AbstractButton::keydown_event(KeyEvent& event) { if (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Space) { - click(event.modifiers()); + m_being_pressed = true; + update(); + event.accept(); + return; + } else if (m_being_pressed && event.key() == KeyCode::Key_Escape) { + m_being_pressed = false; + update(); event.accept(); return; } Widget::keydown_event(event); } +void AbstractButton::keyup_event(KeyEvent& event) +{ + if (m_being_pressed && (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Space)) { + click(event.modifiers()); + event.accept(); + return; + } + Widget::keyup_event(event); +} + void AbstractButton::paint_text(Painter& painter, const Gfx::IntRect& rect, const Gfx::Font& font, Gfx::TextAlignment text_alignment) { auto clipped_rect = rect.intersected(this->rect()); diff --git a/Userland/Libraries/LibGUI/AbstractButton.h b/Userland/Libraries/LibGUI/AbstractButton.h index 43077a2c8e..bcf450e655 100644 --- a/Userland/Libraries/LibGUI/AbstractButton.h +++ b/Userland/Libraries/LibGUI/AbstractButton.h @@ -46,6 +46,7 @@ protected: virtual void mousemove_event(MouseEvent&) override; virtual void mouseup_event(MouseEvent&) override; virtual void keydown_event(KeyEvent&) override; + virtual void keyup_event(KeyEvent&) override; virtual void enter_event(Core::Event&) override; virtual void leave_event(Core::Event&) override; virtual void change_event(Event&) override;