Skip to content

Commit

Permalink
Qt: Support binding numpad keys independently of number row
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek authored and refractionpcsx2 committed Jun 21, 2022
1 parent e4554fe commit 64d222a
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 560 deletions.
6 changes: 4 additions & 2 deletions pcsx2-qt/DisplayWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "EmuThread.h"
#include "MainWindow.h"
#include "QtHost.h"
#include "QtUtils.h"

#include "pcsx2/GS/GSIntrin.h" // _BitScanForward

Expand Down Expand Up @@ -234,7 +235,8 @@ bool DisplayWidget::event(QEvent* event)
// but I can't think of a better way of handling it, and there doesn't appear to be
// any window flag which changes this behavior that I can see.

const int key = key_event->key();
const u32 key = QtUtils::KeyEventToCode(key_event);
const Qt::KeyboardModifiers modifiers = key_event->modifiers();
const bool pressed = (key_event->type() == QEvent::KeyPress);
const auto it = std::find(m_keys_pressed_with_modifiers.begin(), m_keys_pressed_with_modifiers.end(), key);
if (it != m_keys_pressed_with_modifiers.end())
Expand All @@ -244,7 +246,7 @@ bool DisplayWidget::event(QEvent* event)
else
m_keys_pressed_with_modifiers.erase(it);
}
else if (key_event->modifiers() != Qt::NoModifier && pressed)
else if (modifiers != Qt::NoModifier && modifiers != Qt::KeypadModifier && pressed)
{
m_keys_pressed_with_modifiers.push_back(key);
}
Expand Down
41 changes: 36 additions & 5 deletions pcsx2-qt/QtKeyCodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@

#include "PrecompiledHeader.h"

#include "QtUtils.h"

#include "pcsx2/Frontend/InputManager.h"

#include "common/StringUtil.h"

#include <QtGui/QKeyEvent>

struct KeyCodeName
{
int code;
Expand Down Expand Up @@ -465,22 +471,47 @@ static constexpr KeyCodeName s_qt_key_names[] = {

std::optional<u32> InputManager::ConvertHostKeyboardStringToCode(const std::string_view& str)
{
std::string compare_name(str);
u32 modifier_bits = 0;
if (StringUtil::StartsWith(compare_name, "Numpad"))
{
compare_name = compare_name.substr(6);
modifier_bits |= Qt::KeypadModifier;
}

for (const KeyCodeName& name : s_qt_key_names)
{
if (str == name.name)
return static_cast<u32>(name.code);
if (compare_name == name.name)
return static_cast<u32>(name.code) | modifier_bits;
}

return std::nullopt;
}

std::optional<std::string> InputManager::ConvertHostKeyboardCodeToString(u32 code)
{
std::optional<std::string> ret;

const u32 modifier_bits = (code & Qt::KeyboardModifierMask);
const u32 masked_code = (code & ~Qt::KeyboardModifierMask);

for (const KeyCodeName& name : s_qt_key_names)
{
if (static_cast<int>(code) == name.code)
return std::string(name.name);
if (static_cast<int>(masked_code) == name.code)
{
if (modifier_bits & Qt::KeypadModifier)
ret = fmt::format("Numpad{}", name.name);
else
ret = std::string(name.name);

break;
}
}

return std::nullopt;
return ret;
}

u32 QtUtils::KeyEventToCode(const QKeyEvent* ev)
{
return static_cast<u32>(ev->key()) | (static_cast<u32>(ev->modifiers()) & static_cast<u32>(Qt::KeypadModifier));
}
Loading

0 comments on commit 64d222a

Please sign in to comment.