Key control: correctly print UTF-8

This commit is contained in:
Martin Pulec
2019-07-09 09:43:29 +02:00
parent 837d46545a
commit 23c33399ec

View File

@@ -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();