keyboard pads: release all keys on FocusOut event

This commit is contained in:
Megamouse 2021-04-17 11:36:09 +02:00
parent 5416f60643
commit 691b986e36
2 changed files with 41 additions and 12 deletions

View File

@ -106,7 +106,7 @@ void keyboard_pad_handler::Key(const u32 code, bool pressed, u16 value)
} }
} }
for (int i = 0; i < static_cast<int>(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_max = pad->m_sticks[i].m_keyCodeMax == code;
const bool is_min = pad->m_sticks[i].m_keyCodeMin == 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) bool keyboard_pad_handler::eventFilter(QObject* target, QEvent* ev)
{ {
// !m_target is for future proofing when gsrender isn't automatically initialized on load. // !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: case QEvent::Wheel:
mouseWheelEvent(static_cast<QWheelEvent*>(ev)); mouseWheelEvent(static_cast<QWheelEvent*>(ev));
break; break;
case QEvent::FocusOut:
release_all_keys();
break;
default: default:
break; break;
} }
@ -228,6 +252,9 @@ void keyboard_pad_handler::processKeyEvent(QKeyEvent* event, bool pressed)
case Qt::Key_F12: case Qt::Key_F12:
break; break;
case Qt::Key_L: case Qt::Key_L:
if (event->modifiers() != Qt::AltModifier && event->modifiers() != Qt::ControlModifier)
handle_key();
break;
case Qt::Key_Return: case Qt::Key_Return:
if (event->modifiers() != Qt::AltModifier) if (event->modifiers() != Qt::AltModifier)
handle_key(); handle_key();
@ -333,8 +360,6 @@ void keyboard_pad_handler::mouseMoveEvent(QMouseEvent* event)
static int movement_x = 0; static int movement_x = 0;
static int movement_y = 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()) if (m_target && m_target->isActive() && get_mouse_lock_state())
{ {
@ -358,6 +383,9 @@ void keyboard_pad_handler::mouseMoveEvent(QMouseEvent* event)
} }
else else
{ {
static int last_pos_x = 0;
static int last_pos_y = 0;
movement_x = event->x() - last_pos_x; movement_x = event->x() - last_pos_x;
movement_y = event->y() - last_pos_y; movement_y = event->y() - last_pos_y;
@ -589,17 +617,17 @@ u32 keyboard_pad_handler::GetKeyCode(const QString& keyName)
{ {
if (keyName.isEmpty()) if (keyName.isEmpty())
return 0; 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 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; return Qt::Key_Alt;
else if (keyName == "AltGr") if (keyName == "AltGr")
return Qt::Key_AltGr; return Qt::Key_AltGr;
else if (keyName == "Shift") if (keyName == "Shift")
return Qt::Key_Shift; return Qt::Key_Shift;
else if (keyName == "Ctrl") if (keyName == "Ctrl")
return Qt::Key_Control; return Qt::Key_Control;
else if (keyName == "Meta") if (keyName == "Meta")
return Qt::Key_Meta; return Qt::Key_Meta;
const QKeySequence seq(keyName); const QKeySequence seq(keyName);
@ -619,11 +647,11 @@ int keyboard_pad_handler::native_scan_code_from_string([[maybe_unused]] const st
#ifdef _WIN32 #ifdef _WIN32
if (key == "Shift Left") if (key == "Shift Left")
return 42; return 42;
else if (key == "Shift Right") if (key == "Shift Right")
return 54; return 54;
else if (key == "Ctrl Left") if (key == "Ctrl Left")
return 29; return 29;
else if (key == "Ctrl Right") if (key == "Ctrl Right")
return 285; return 285;
#else #else
// TODO // TODO

View File

@ -106,6 +106,7 @@ private:
QWindow* m_target = nullptr; QWindow* m_target = nullptr;
bool m_mouse_move_used = false; bool m_mouse_move_used = false;
bool get_mouse_lock_state() const; bool get_mouse_lock_state() const;
void release_all_keys();
std::vector<std::shared_ptr<Pad>> m_bindings; std::vector<std::shared_ptr<Pad>> m_bindings;