From 691b986e36de77f3292400c718fe0947228fce9c Mon Sep 17 00:00:00 2001 From: Megamouse Date: Sat, 17 Apr 2021 11:36:09 +0200 Subject: [PATCH] keyboard pads: release all keys on FocusOut event --- rpcs3/Input/keyboard_pad_handler.cpp | 52 +++++++++++++++++++++------- rpcs3/Input/keyboard_pad_handler.h | 1 + 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/rpcs3/Input/keyboard_pad_handler.cpp b/rpcs3/Input/keyboard_pad_handler.cpp index 8c356fd05b..67881fc9ab 100644 --- a/rpcs3/Input/keyboard_pad_handler.cpp +++ b/rpcs3/Input/keyboard_pad_handler.cpp @@ -106,7 +106,7 @@ void keyboard_pad_handler::Key(const u32 code, bool pressed, u16 value) } } - for (int i = 0; i < static_cast(pad->m_sticks.size()); i++) + for (usz i = 0; i < pad->m_sticks.size(); i++) { const bool is_max = pad->m_sticks[i].m_keyCodeMax == code; const bool is_min = pad->m_sticks[i].m_keyCodeMin == code; @@ -135,6 +135,27 @@ void keyboard_pad_handler::Key(const u32 code, bool pressed, u16 value) } } +void keyboard_pad_handler::release_all_keys() +{ + for (const auto& pad : m_bindings) + { + for (Button& button : pad->m_buttons) + { + button.m_pressed = false; + button.m_value = 0; + button.m_actual_value = 0; + } + + for (usz i = 0; i < pad->m_sticks.size(); i++) + { + m_stick_min[i] = 0; + m_stick_max[i] = 128; + m_stick_val[i] = 128; + pad->m_sticks[i].m_value = 128; + } + } +} + bool keyboard_pad_handler::eventFilter(QObject* target, QEvent* ev) { // !m_target is for future proofing when gsrender isn't automatically initialized on load. @@ -161,6 +182,9 @@ bool keyboard_pad_handler::eventFilter(QObject* target, QEvent* ev) case QEvent::Wheel: mouseWheelEvent(static_cast(ev)); break; + case QEvent::FocusOut: + release_all_keys(); + break; default: break; } @@ -228,6 +252,9 @@ void keyboard_pad_handler::processKeyEvent(QKeyEvent* event, bool pressed) case Qt::Key_F12: break; case Qt::Key_L: + if (event->modifiers() != Qt::AltModifier && event->modifiers() != Qt::ControlModifier) + handle_key(); + break; case Qt::Key_Return: if (event->modifiers() != Qt::AltModifier) handle_key(); @@ -333,8 +360,6 @@ void keyboard_pad_handler::mouseMoveEvent(QMouseEvent* event) static int movement_x = 0; static int movement_y = 0; - static int last_pos_x = 0; - static int last_pos_y = 0; if (m_target && m_target->isActive() && get_mouse_lock_state()) { @@ -358,6 +383,9 @@ void keyboard_pad_handler::mouseMoveEvent(QMouseEvent* event) } else { + static int last_pos_x = 0; + static int last_pos_y = 0; + movement_x = event->x() - last_pos_x; movement_y = event->y() - last_pos_y; @@ -589,17 +617,17 @@ u32 keyboard_pad_handler::GetKeyCode(const QString& keyName) { if (keyName.isEmpty()) return 0; - else if (const int native_scan_code = native_scan_code_from_string(sstr(keyName)); native_scan_code >= 0) + if (const int native_scan_code = native_scan_code_from_string(sstr(keyName)); native_scan_code >= 0) return Qt::Key_unknown + native_scan_code; // Special cases that can't be expressed with Qt::Key - else if (keyName == "Alt") + if (keyName == "Alt") return Qt::Key_Alt; - else if (keyName == "AltGr") + if (keyName == "AltGr") return Qt::Key_AltGr; - else if (keyName == "Shift") + if (keyName == "Shift") return Qt::Key_Shift; - else if (keyName == "Ctrl") + if (keyName == "Ctrl") return Qt::Key_Control; - else if (keyName == "Meta") + if (keyName == "Meta") return Qt::Key_Meta; const QKeySequence seq(keyName); @@ -619,11 +647,11 @@ int keyboard_pad_handler::native_scan_code_from_string([[maybe_unused]] const st #ifdef _WIN32 if (key == "Shift Left") return 42; - else if (key == "Shift Right") + if (key == "Shift Right") return 54; - else if (key == "Ctrl Left") + if (key == "Ctrl Left") return 29; - else if (key == "Ctrl Right") + if (key == "Ctrl Right") return 285; #else // TODO diff --git a/rpcs3/Input/keyboard_pad_handler.h b/rpcs3/Input/keyboard_pad_handler.h index 496b055321..5e4f86ceec 100644 --- a/rpcs3/Input/keyboard_pad_handler.h +++ b/rpcs3/Input/keyboard_pad_handler.h @@ -106,6 +106,7 @@ private: QWindow* m_target = nullptr; bool m_mouse_move_used = false; bool get_mouse_lock_state() const; + void release_all_keys(); std::vector> m_bindings;