Fixed Chinese IME support (thanks ???!)

This commit is contained in:
Sam Lantinga 2018-09-10 23:01:33 -07:00
parent af823cc1d9
commit 0b3a350c90

View File

@ -74,6 +74,8 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o
#if SDL_IPHONE_KEYBOARD #if SDL_IPHONE_KEYBOARD
UITextField *textField; UITextField *textField;
BOOL rotatingOrientation; BOOL rotatingOrientation;
NSString *changeText;
NSString *obligateForBackspace;
#endif #endif
} }
@ -250,10 +252,12 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o
/* Set ourselves up as a UITextFieldDelegate */ /* Set ourselves up as a UITextFieldDelegate */
- (void)initKeyboard - (void)initKeyboard
{ {
changeText = nil;
obligateForBackspace = @" "; /* 64 space */
textField = [[UITextField alloc] initWithFrame:CGRectZero]; textField = [[UITextField alloc] initWithFrame:CGRectZero];
textField.delegate = self; textField.delegate = self;
/* placeholder so there is something to delete! */ /* placeholder so there is something to delete! */
textField.text = @" "; textField.text = obligateForBackspace;
/* set UITextInputTrait properties, mostly to defaults */ /* set UITextInputTrait properties, mostly to defaults */
textField.autocapitalizationType = UITextAutocapitalizationTypeNone; textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
@ -267,11 +271,12 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o
textField.hidden = YES; textField.hidden = YES;
keyboardVisible = NO; keyboardVisible = NO;
#if !TARGET_OS_TV
NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
#if !TARGET_OS_TV
[center addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [center addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[center addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; [center addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
#endif #endif
[center addObserver:self selector:@selector(textFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
} }
- (void)setView:(UIView *)view - (void)setView:(UIView *)view
@ -310,11 +315,12 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o
- (void)deinitKeyboard - (void)deinitKeyboard
{ {
#if !TARGET_OS_TV
NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
#if !TARGET_OS_TV
[center removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [center removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[center removeObserver:self name:UIKeyboardWillHideNotification object:nil]; [center removeObserver:self name:UIKeyboardWillHideNotification object:nil];
#endif #endif
[center removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
} }
/* reveal onscreen virtual keyboard */ /* reveal onscreen virtual keyboard */
@ -354,6 +360,50 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o
[self setKeyboardHeight:0]; [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 - (void)updateKeyboard
{ {
CGAffineTransform t = self.view.transform; CGAffineTransform t = self.view.transform;
@ -392,49 +442,20 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string - (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{ {
NSUInteger len = string.length; NSUInteger len = string.length;
if (len == 0) { if (len == 0) {
changeText = nil;
if (textField.markedTextRange == nil) {
/* it wants to replace text with nothing, ie a delete */ /* it wants to replace text with nothing, ie a delete */
SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_BACKSPACE); SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_BACKSPACE);
SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_BACKSPACE); SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_BACKSPACE);
}
if (textField.text.length < 16) {
textField.text = obligateForBackspace;
}
} else { } else {
/* go through all the characters in the string we've been sent and changeText = string;
* 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;
} }
return YES;
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]);
}
return NO; /* don't allow the edit! (keep placeholder text there) */
} }
/* Terminates the editing session */ /* Terminates the editing session */
@ -498,7 +519,7 @@ UIKit_IsScreenKeyboardShown(_THIS, SDL_Window *window)
@autoreleasepool { @autoreleasepool {
SDL_uikitviewcontroller *vc = GetWindowViewController(window); SDL_uikitviewcontroller *vc = GetWindowViewController(window);
if (vc != nil) { if (vc != nil) {
return vc.isKeyboardVisible; return vc.keyboardVisible;
} }
return SDL_FALSE; return SDL_FALSE;
} }