From ad5834b1e154f615e62c40631eee87852078a74d Mon Sep 17 00:00:00 2001 From: Igor Stepanov Date: Fri, 10 Oct 2025 14:56:34 +0300 Subject: [PATCH] fix utf8 input --- src/window/detail/input_sdl.cpp | 20 +++++++++++--------- src/window/detail/input_sdl.hpp | 4 ++-- src/window/input.hpp | 6 ++---- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/window/detail/input_sdl.cpp b/src/window/detail/input_sdl.cpp index bd6a8830f..d45125fa6 100644 --- a/src/window/detail/input_sdl.cpp +++ b/src/window/detail/input_sdl.cpp @@ -9,9 +9,10 @@ #include #include -#include +#include #include "debug/Logger.hpp" +#include "util/stringutil.hpp" #include "window/detail/window_sdl.hpp" #include "window/input.hpp" @@ -132,6 +133,9 @@ void input_sdl::pollEvents() { codepoints.clear(); pressedKeys.clear(); + std::string text {}; + uint size {}; + bool prevPressed = false; static SDL_Event event; @@ -184,15 +188,13 @@ void input_sdl::pollEvents() { window.setSize({event.window.data1, event.window.data2}); break; case SDL_EVENT_TEXT_INPUT: - std::vector vec( - event.text.text, - event.text.text + std::strlen(event.text.text) - ); - std::copy( - vec.begin(), vec.end(), std::back_inserter(codepoints) - ); + text += event.text.text; } } + if (!text.empty()) { + codepoints.push_back(util::decode_utf8(size, text.c_str())); + } + for (auto& [_, binding] : bindings.getAll()) { if (!binding.enabled) { binding.state = false; @@ -288,6 +290,6 @@ const std::vector& input_sdl::getPressedKeys() const { return pressedKeys; } -const std::vector& input_sdl::getCodepoints() const { +const std::vector& input_sdl::getCodepoints() const { return codepoints; } diff --git a/src/window/detail/input_sdl.hpp b/src/window/detail/input_sdl.hpp index 5b8c5bd03..a524f2023 100644 --- a/src/window/detail/input_sdl.hpp +++ b/src/window/detail/input_sdl.hpp @@ -38,7 +38,7 @@ public: ObserverHandler addKeyCallback(Keycode key, KeyCallback callback) override; const std::vector& getPressedKeys() const override; - const std::vector& getCodepoints() const override; + const std::vector& getCodepoints() const override; private: glm::vec2 delta; glm::vec2 cursor; @@ -49,7 +49,7 @@ private: bool cursorLocked = false; bool cursorDrag = false; Bindings bindings; - std::vector codepoints; + std::vector codepoints; std::vector pressedKeys; std::unordered_map> keyCallbacks; diff --git a/src/window/input.hpp b/src/window/input.hpp index 7ae082dd3..6e4c2a6c9 100644 --- a/src/window/input.hpp +++ b/src/window/input.hpp @@ -6,7 +6,6 @@ #include "util/HandlersList.hpp" - namespace dv { class value; } @@ -124,7 +123,6 @@ inline Mousecode mousecodes_all[] { Mousecode::BUTTON_5, }; - namespace input_util { void initialize(); Keycode keycode_from(const std::string& name); @@ -134,7 +132,7 @@ namespace input_util { std::string to_string(Keycode code); /// @return Mouse button label by keycode std::string to_string(Mousecode code); - + /// @return Key name by keycode std::string get_name(Keycode code); /// @return Mouse button name by keycode @@ -282,7 +280,7 @@ public: ) = 0; virtual const std::vector& getPressedKeys() const = 0; - virtual const std::vector& getCodepoints() const = 0; + virtual const std::vector& getCodepoints() const = 0; ObserverHandler addCallback(const std::string& name, KeyCallback callback) { return getBindings().require(name).onactived.add(callback);