diff --git a/src/inferno/application.cpp b/src/inferno/application.cpp index 039517d..3d196d4 100644 --- a/src/inferno/application.cpp +++ b/src/inferno/application.cpp @@ -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")) { diff --git a/src/inferno/keycodes.cpp b/src/inferno/keycodes.cpp index 75a8da6..6f550b1 100644 --- a/src/inferno/keycodes.cpp +++ b/src/inferno/keycodes.cpp @@ -1,4 +1,5 @@ -#include // std::string +#include // std::find_if +#include #include // std::unordered_map #include "GLFW/glfw3.h" @@ -8,7 +9,7 @@ namespace Inferno { -static std::unordered_map keys({ +static std::unordered_map keys({ { MAP_KEY(GLFW_KEY_UNKNOWN) }, { MAP_KEY(GLFW_KEY_SPACE) }, { MAP_KEY(GLFW_KEY_APOSTROPHE) }, @@ -134,10 +135,20 @@ static std::unordered_map 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 diff --git a/src/inferno/keycodes.h b/src/inferno/keycodes.h index b0892bc..2f24f75 100644 --- a/src/inferno/keycodes.h +++ b/src/inferno/keycodes.h @@ -1,9 +1,12 @@ #pragma once +#include + #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