[Commits] SDL: Fixed Chinese IME support (thanks 树子。!)

libsdl.org revision control commits-owner at libsdl.org
Mon Sep 10 23:01:42 PDT 2018


details:   https://hg.libsdl.org/SDL/rev/02509665751f
changeset: 12184:02509665751f
user:      Sam Lantinga <slouken at libsdl.org>
date:      Mon Sep 10 23:01:33 2018 -0700
description:
Fixed Chinese IME support (thanks 树子。!)

diffstat:

 src/video/uikit/SDL_uikitviewcontroller.m |  109 +++++++++++++++++------------
 1 files changed, 65 insertions(+), 44 deletions(-)

diffs (175 lines):

diff -r 364f514f94d8 -r 02509665751f src/video/uikit/SDL_uikitviewcontroller.m
--- a/src/video/uikit/SDL_uikitviewcontroller.m	Mon Sep 10 23:00:09 2018 -0700
+++ b/src/video/uikit/SDL_uikitviewcontroller.m	Mon Sep 10 23:01:33 2018 -0700
@@ -74,6 +74,8 @@
 #if SDL_IPHONE_KEYBOARD
     UITextField *textField;
     BOOL rotatingOrientation;
+    NSString *changeText;
+    NSString *obligateForBackspace;
 #endif
 }
 
@@ -250,10 +252,12 @@
 /* Set ourselves up as a UITextFieldDelegate */
 - (void)initKeyboard
 {
+    changeText = nil;
+    obligateForBackspace = @"                                                                "; /* 64 space */
     textField = [[UITextField alloc] initWithFrame:CGRectZero];
     textField.delegate = self;
     /* placeholder so there is something to delete! */
-    textField.text = @" ";
+    textField.text = obligateForBackspace;
 
     /* set UITextInputTrait properties, mostly to defaults */
     textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
@@ -267,11 +271,12 @@
     textField.hidden = YES;
     keyboardVisible = NO;
 
+    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
 #if !TARGET_OS_TV
-    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
     [center addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
     [center addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
 #endif
+    [center addObserver:self selector:@selector(textFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
 }
 
 - (void)setView:(UIView *)view
@@ -310,11 +315,12 @@
 
 - (void)deinitKeyboard
 {
+    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
 #if !TARGET_OS_TV
-    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
     [center removeObserver:self name:UIKeyboardWillShowNotification object:nil];
     [center removeObserver:self name:UIKeyboardWillHideNotification object:nil];
 #endif
+    [center removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
 }
 
 /* reveal onscreen virtual keyboard */
@@ -354,6 +360,50 @@
     [self setKeyboardHeight:0];
 }
 
+- (void)textFieldTextDidChange:(NSNotification *)notification
+{
+    if (changeText!=nil && textField.markedTextRange == nil)
+    {
+        NSUInteger len = changeText.length;
+        if (len > 0) {
+            /* Go through all the characters in the string we've been sent and
+             * convert them to key presses */
+            int i;
+            for (i = 0; i < len; i++) {
+                unichar c = [changeText characterAtIndex:i];
+                SDL_Scancode code;
+                Uint16 mod;
+
+                if (c < 127) {
+                    /* Figure out the SDL_Scancode and SDL_keymod for this unichar */
+                    code = unicharToUIKeyInfoTable[c].code;
+                    mod  = unicharToUIKeyInfoTable[c].mod;
+                } else {
+                    /* We only deal with ASCII right now */
+                    code = SDL_SCANCODE_UNKNOWN;
+                    mod = 0;
+                }
+
+                if (mod & KMOD_SHIFT) {
+                    /* If character uses shift, press shift down */
+                    SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
+                }
+
+                /* send a keydown and keyup even for the character */
+                SDL_SendKeyboardKey(SDL_PRESSED, code);
+                SDL_SendKeyboardKey(SDL_RELEASED, code);
+
+                if (mod & KMOD_SHIFT) {
+                    /* If character uses shift, press shift back up */
+                    SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
+                }
+            }
+            SDL_SendKeyboardText([changeText UTF8String]);
+        }
+        changeText = nil;
+    }
+}
+
 - (void)updateKeyboard
 {
     CGAffineTransform t = self.view.transform;
@@ -392,49 +442,20 @@
 - (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
 {
     NSUInteger len = string.length;
-
     if (len == 0) {
-        /* it wants to replace text with nothing, ie a delete */
-        SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_BACKSPACE);
-        SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_BACKSPACE);
+        changeText = nil;
+        if (textField.markedTextRange == nil) {
+            /* it wants to replace text with nothing, ie a delete */
+            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_BACKSPACE);
+            SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_BACKSPACE);
+        }
+        if (textField.text.length < 16) {
+            textField.text = obligateForBackspace;
+        }
     } else {
-        /* go through all the characters in the string we've been sent and
-         * convert them to key presses */
-        int i;
-        for (i = 0; i < len; i++) {
-            unichar c = [string characterAtIndex:i];
-            Uint16 mod = 0;
-            SDL_Scancode code;
-
-            if (c < 127) {
-                /* figure out the SDL_Scancode and SDL_keymod for this unichar */
-                code = unicharToUIKeyInfoTable[c].code;
-                mod  = unicharToUIKeyInfoTable[c].mod;
-            } else {
-                /* we only deal with ASCII right now */
-                code = SDL_SCANCODE_UNKNOWN;
-                mod = 0;
-            }
-
-            if (mod & KMOD_SHIFT) {
-                /* If character uses shift, press shift down */
-                SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
-            }
-
-            /* send a keydown and keyup even for the character */
-            SDL_SendKeyboardKey(SDL_PRESSED, code);
-            SDL_SendKeyboardKey(SDL_RELEASED, code);
-
-            if (mod & KMOD_SHIFT) {
-                /* If character uses shift, press shift back up */
-                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
-            }
-        }
-
-        SDL_SendKeyboardText([string UTF8String]);
+        changeText = string;
     }
-
-    return NO; /* don't allow the edit! (keep placeholder text there) */
+    return YES;
 }
 
 /* Terminates the editing session */
@@ -498,7 +519,7 @@
     @autoreleasepool {
         SDL_uikitviewcontroller *vc = GetWindowViewController(window);
         if (vc != nil) {
-            return vc.isKeyboardVisible;
+            return vc.keyboardVisible;
         }
         return SDL_FALSE;
     }


More information about the commits mailing list