From 144d10b682bb411c143fb2075561bd7a97fb1ebf Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Thu, 15 Feb 2018 14:46:44 +0100 Subject: [PATCH] Support Unicode Keyboard Input --- src/global.h | 2 ++ src/input.c | 4 ++++ src/input.h | 4 ++-- src/keyboard.c | 4 +++- src/readline.c | 4 ++-- 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/global.h b/src/global.h index d8bb6974..fdf28ddf 100644 --- a/src/global.h +++ b/src/global.h @@ -97,6 +97,8 @@ GFX2_GLOBAL dword Key; /// This is mostly used when the user enters text (filename, etc). GFX2_GLOBAL dword Key_ANSI; +GFX2_GLOBAL dword Key_UNICODE; + // Keyboard modifiers // (Name conflict with windows.h) #ifdef MOD_SHIFT diff --git a/src/input.c b/src/input.c index ced4feb7..aec9243d 100644 --- a/src/input.c +++ b/src/input.c @@ -383,6 +383,9 @@ int Handle_key_press(SDL_KeyboardEvent event) Key = Keysym_to_keycode(event.keysym); Key_ANSI = Keysym_to_ANSI(event.keysym); + Key_UNICODE = event.keysym.unicode; + if (Key_UNICODE == 0) + Key_UNICODE = Key_ANSI; switch(event.keysym.sym) { case SDLK_RSHIFT: @@ -847,6 +850,7 @@ int Get_input(int sleep_time) // some user input. Flush_update(); Key_ANSI = 0; + Key_UNICODE = 0; Key = 0; Mouse_moved=0; Input_new_mouse_X = Mouse_X; diff --git a/src/input.h b/src/input.h index 70aec7a6..427d4356 100644 --- a/src/input.h +++ b/src/input.h @@ -30,8 +30,8 @@ /// This is the keyboard/mouse/joystick input polling function. /// Returns 1 if a significant changed occurred, such as a mouse button pressed /// or depressed, or a new keypress was in the keyboard buffer. -/// The latest input variables are held in ::Key, ::Key_ANSI, ::Mouse_X, ::Mouse_Y, ::Mouse_K. -/// Note that ::Key and ::Key_ANSI are not persistent, they will be reset to 0 +/// The latest input variables are held in ::Key, ::Key_ANSI, ::Key_UNICODE, ::Mouse_X, ::Mouse_Y, ::Mouse_K. +/// Note that ::Key, ::Key_ANSI and ::Key_UNICODE are not persistent, they will be reset to 0 /// on subsequent calls to ::Get_input(). int Get_input(int sleep_time); diff --git a/src/keyboard.c b/src/keyboard.c index 8dede28f..b7dccc92 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -633,7 +633,7 @@ word Keysym_to_ANSI(SDL_keysym keysym) // This part was removed from the MacOSX port, but I put it back for others // as on Linux and Windows, it's what allows editing a text line with the keys // SDLK_LEFT, SDLK_RIGHT, SDLK_HOME, SDLK_END etc. - #if !(defined(__macosx__) || defined(__FreeBSD__)) + #if !defined(__macosx__) if ( keysym.unicode == 0) { @@ -722,6 +722,8 @@ word Keysym_to_ANSI(SDL_keysym keysym) return 'º'; // º case 0xC600: return 'ã'; // ã + case 0x20AC: + return '\x80'; // Euro sign is 20AC in unicode, 80 in CP1252 } // Key entre 127 et 255 diff --git a/src/readline.c b/src/readline.c index 11b73b79..ab5eefd0 100644 --- a/src/readline.c +++ b/src/readline.c @@ -172,7 +172,7 @@ int Valid_character(word c, int input_type) #endif int position; - if (c < ' ' || c > 255) + if (c < ' ') return 0; for (position=0; position<(long)sizeof(forbidden_char); position++) @@ -649,7 +649,7 @@ byte Readline_ex_unicode(word x_pos,word y_pos,char * str,word * str_unicode,byt do { Get_input(20); - input_key=Key_ANSI; + input_key = (str_unicode == NULL) ? Key_ANSI : Key_UNICODE; if (Mouse_K) input_key=SDLK_RETURN;