From 8e632130b827f5542020cd820d2efe60f4cf07b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 16 Aug 2016 19:14:25 +0200 Subject: [PATCH] Platform: added *Application::KeyEvent::keyName(). The SDL2 variant works better than expected, however the GLFW variant underdelivers -- no key names for modifier keys, the accent keys are not UTF-8... I don't care ATM, will solve it once someone actually complains. --- src/Magnum/Platform/GlfwApplication.cpp | 10 +++++++++ src/Magnum/Platform/GlfwApplication.h | 8 +++++++- src/Magnum/Platform/Sdl2Application.cpp | 8 ++++++++ src/Magnum/Platform/Sdl2Application.h | 27 ++++++++++++++++++++++++- 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/Magnum/Platform/GlfwApplication.cpp b/src/Magnum/Platform/GlfwApplication.cpp index c17b8b477..3b65d9450 100644 --- a/src/Magnum/Platform/GlfwApplication.cpp +++ b/src/Magnum/Platform/GlfwApplication.cpp @@ -27,6 +27,7 @@ #include "GlfwApplication.h" #include +#include #include "Magnum/Version.h" #include "Magnum/Platform/Context.h" @@ -245,6 +246,15 @@ GlfwApplication::Configuration::Configuration(): GlfwApplication::Configuration::~Configuration() = default; +std::string GlfwApplication::KeyEvent::keyName(const Key key) { + /* It can return null, so beware */ + return Utility::String::fromArray(glfwGetKeyName(int(key), 0)); +} + +std::string GlfwApplication::KeyEvent::keyName() const { + return keyName(_key); +} + template class BasicScreen; template class BasicScreenedApplication; diff --git a/src/Magnum/Platform/GlfwApplication.h b/src/Magnum/Platform/GlfwApplication.h index 63c44e4a0..af7a002bf 100644 --- a/src/Magnum/Platform/GlfwApplication.h +++ b/src/Magnum/Platform/GlfwApplication.h @@ -744,9 +744,15 @@ class GlfwApplication::KeyEvent: public GlfwApplication::InputEvent { Menu = GLFW_KEY_MENU /**< Menu */ }; - /** @brief Key */ + /** @copydoc Sdl2Application::KeyEvent::keyName(Key) */ + static std::string keyName(Key key); + + /** @copydoc Sdl2Application::KeyEvent::key() */ constexpr Key key() const { return _key; } + /** @copydoc Sdl2Application::KeyEvent::keyName() const */ + std::string keyName() const; + /** @brief Modifiers */ constexpr Modifiers modifiers() const { return _modifiers; } diff --git a/src/Magnum/Platform/Sdl2Application.cpp b/src/Magnum/Platform/Sdl2Application.cpp index 881652409..30b6ec26f 100644 --- a/src/Magnum/Platform/Sdl2Application.cpp +++ b/src/Magnum/Platform/Sdl2Application.cpp @@ -552,6 +552,14 @@ Sdl2Application::Configuration::Configuration(): Sdl2Application::Configuration::~Configuration() = default; +std::string Sdl2Application::KeyEvent::keyName(const Key key) { + return SDL_GetKeyName(SDL_Keycode(key)); +} + +std::string Sdl2Application::KeyEvent::keyName() const { + return keyName(_key); +} + Sdl2Application::InputEvent::Modifiers Sdl2Application::MouseEvent::modifiers() { if(_modifiersLoaded) return _modifiers; _modifiersLoaded = true; diff --git a/src/Magnum/Platform/Sdl2Application.h b/src/Magnum/Platform/Sdl2Application.h index cf2d4e01d..751c478e9 100644 --- a/src/Magnum/Platform/Sdl2Application.h +++ b/src/Magnum/Platform/Sdl2Application.h @@ -1212,9 +1212,34 @@ class Sdl2Application::KeyEvent: public Sdl2Application::InputEvent { Z = SDLK_z /**< Letter Z */ }; - /** @brief Key */ + /** + * @brief Name for given key + * + * Human-readable localized UTF-8 name for given @p key, intended for + * displaying to the user in e.g. key binding configuration. If there + * is no name for given key, empty string is returned. + * @see @ref keyName(Key) + */ + static std::string keyName(Key key); + + /** + * @brief Key + * + * @see @ref keyName() + */ constexpr Key key() const { return _key; } + /** + * @brief Key name + * + * Human-readable localized UTF-8 name for the key returned by + * @ref key(), intended for displaying to the user in e.g. + * key binding configuration. If there is no name for that key, empty + * string is returned. + * @see @ref keyName(Key) + */ + std::string keyName() const; + /** @brief Modifiers */ constexpr Modifiers modifiers() const { return _modifiers; }