Engine: Add function for finding key names

This commit is contained in:
Riyyi
2022-09-26 13:00:00 +02:00
parent 8a03d84580
commit f44c82d06e
3 changed files with 21 additions and 7 deletions
+1 -1
View File
@@ -199,7 +199,7 @@ bool Application::onKeyPress(KeyPressEvent& e)
// Suppress unused warning
(void)e;
ruc::info("KeyPressEvent {} ({})", Input::getKeyName(e.getKey()), e.getKey());
ruc::info("KeyPressEvent {:3} {}", e.getKey(), keyName(e.getKey()));
// Stop the main loop on 'Escape' keypress
if (e.getKey() == keyCode("GLFW_KEY_ESCAPE")) {
+15 -4
View File
@@ -1,4 +1,5 @@
#include <string> // std::string
#include <algorithm> // std::find_if
#include <string_view>
#include <unordered_map> // std::unordered_map
#include "GLFW/glfw3.h"
@@ -8,7 +9,7 @@
namespace Inferno {
static std::unordered_map<std::string, int> keys({
static std::unordered_map<std::string_view, int> keys({
{ MAP_KEY(GLFW_KEY_UNKNOWN) },
{ MAP_KEY(GLFW_KEY_SPACE) },
{ MAP_KEY(GLFW_KEY_APOSTROPHE) },
@@ -134,10 +135,20 @@ static std::unordered_map<std::string, int> keys({
// -----------------------------------------
int keyCode(const char* name)
int keyCode(std::string_view name)
{
VERIFY(keys.find(name) != keys.end(), "keyCode could not find '{}'", name);
VERIFY(keys.find(name) != keys.end(), "could not find key code: {}", name);
return keys.at(name);
}
std::string_view keyName(int key)
{
auto it = std::find_if(keys.begin(), keys.end(), [key](const auto& keybind) {
return keybind.second == key;
});
VERIFY(it != keys.end(), "could not find key name: {}", key);
return it->first.substr(9);
}
} // namespace Inferno
+5 -2
View File
@@ -1,9 +1,12 @@
#pragma once
#include <string_view>
#define MAP_KEY(key) #key, key
namespace Inferno {
int keyCode(const char* name);
int keyCode(std::string_view name);
std::string_view keyName(int);
}
} // namespace Inferno