From 23c33399ecfebb60600ce5ae7cf336ba702d934a Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Tue, 9 Jul 2019 09:43:29 +0200 Subject: [PATCH] Key control: correctly print UTF-8 --- src/keyboard_control.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/keyboard_control.cpp b/src/keyboard_control.cpp index 386d252e3..e0a323154 100644 --- a/src/keyboard_control.cpp +++ b/src/keyboard_control.cpp @@ -289,6 +289,29 @@ static int64_t convert_win_to_ansi_keycode(int c) { } #endif +static bool is_utf8(int64_t ch) { +#ifdef WIN32 + return false; +#endif + + unsigned char first_byte; + while (ch != 0) { + first_byte = ch & 0xff; + ch >>= 8; + } + return first_byte & 0x80; /// @todo UTF-8 validity check would be nice +} + +static string get_utf8_representation(int64_t ch) { + char str[9] = ""; + while (ch != 0) { + memmove(str + 1, str, 8); + str[0] = ch & 0xff; + ch >>= 8; + } + return str; +} + static string get_keycode_representation(int64_t ch) { switch (ch) { case K_UP: return "KEY_UP"; @@ -303,6 +326,10 @@ static string get_keycode_representation(int64_t ch) { return string(1, ch); } + if (is_utf8(ch)) { + return get_utf8_representation(ch); + } + stringstream oss; oss << "0x" << hex << ch; return oss.str();