Use unordered map to store keyboard keys

This potentially reduces latency ever so slightly.
This commit is contained in:
Megamouse 2024-04-19 01:57:04 +02:00
parent b18a97e777
commit dcb8f21b2b
3 changed files with 17 additions and 15 deletions

View File

@ -46,17 +46,12 @@ void KeyboardHandlerBase::Key(u32 code, bool pressed, const std::u32string& key)
for (Keyboard& keyboard : m_keyboards)
{
bool found_key = false;
KbData& data = keyboard.m_data;
KbConfig& config = keyboard.m_config;
const KbConfig& config = keyboard.m_config;
for (const KbButton& button : keyboard.m_buttons)
if (auto it = keyboard.m_keys.find(code); it != keyboard.m_keys.end())
{
if (button.m_keyCode != code)
continue;
found_key = true;
KbButton& button = it->second;
u16 kcode = CELL_KEYC_NO_EVENT;
bool is_meta_key = IsMetaKey(code);
@ -156,8 +151,7 @@ void KeyboardHandlerBase::Key(u32 code, bool pressed, const std::u32string& key)
}
}
}
if (!found_key && !key.empty())
else if (!key.empty())
{
if (pressed)
{
@ -205,14 +199,14 @@ void KeyboardHandlerBase::ReleaseAllKeys()
{
for (Keyboard& keyboard : m_keyboards)
{
for (const KbButton& button : keyboard.m_buttons)
for (const auto& [key_code, button] : keyboard.m_keys)
{
Key(button.m_keyCode, false, {});
}
for (const std::u32string& key : keyboard.m_extra_data.pressed_keys)
{
Key(0, false, key);
Key(CELL_KEYC_NO_EVENT, false, key);
}
keyboard.m_extra_data.pressed_keys.clear();

View File

@ -5,6 +5,7 @@
#include <mutex>
#include <vector>
#include <set>
#include <unordered_map>
#include "util/init_mutex.hpp"
@ -71,7 +72,7 @@ struct Keyboard
KbData m_data{};
KbExtraData m_extra_data{};
KbConfig m_config{};
std::vector<KbButton> m_buttons;
std::unordered_map<u32, KbButton> m_keys;
};
class KeyboardHandlerBase
@ -95,7 +96,6 @@ public:
KbInfo& GetInfo() { return m_info; }
std::vector<Keyboard>& GetKeyboards() { return m_keyboards; }
std::vector<KbButton>& GetButtons(const u32 keyboard) { return m_keyboards[keyboard].m_buttons; }
KbData& GetData(const u32 keyboard) { return m_keyboards[keyboard].m_data; }
KbExtraData& GetExtraData(const u32 keyboard) { return m_keyboards[keyboard].m_extra_data; }
KbConfig& GetConfig(const u32 keyboard) { return m_keyboards[keyboard].m_config; }

View File

@ -173,7 +173,7 @@ void basic_keyboard_handler::LoadSettings()
return;
}
std::vector<KbButton>& buttons = m_keyboards[0].m_buttons;
std::vector<KbButton> buttons;
// Meta Keys
//buttons.emplace_back(Qt::Key_Control, CELL_KB_MKEY_L_CTRL);
@ -310,4 +310,12 @@ void basic_keyboard_handler::LoadSettings()
buttons.emplace_back(Qt::Key_ssharp, CELL_KEYC_SSHARP);
buttons.emplace_back(Qt::Key_QuoteLeft, CELL_KEYC_BACK_QUOTE);
buttons.emplace_back(Qt::Key_acute, CELL_KEYC_BACK_QUOTE);
// Fill our map
auto& keys = m_keyboards[0].m_keys;
for (const KbButton& button : buttons)
{
ensure(keys.try_emplace(button.m_keyCode, button).second);
}
}