Skip to content

Commit

Permalink
Fixed Chinese IME support (thanks ???!)
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Sep 11, 2018
1 parent af823cc commit 0b3a350
Showing 1 changed file with 65 additions and 44 deletions.
109 changes: 65 additions & 44 deletions src/video/uikit/SDL_uikitviewcontroller.m
Expand Up @@ -74,6 +74,8 @@ @implementation SDL_uikitviewcontroller {
#if SDL_IPHONE_KEYBOARD
UITextField *textField;
BOOL rotatingOrientation;
NSString *changeText;
NSString *obligateForBackspace;
#endif
}

Expand Down Expand Up @@ -250,10 +252,12 @@ - (UIRectEdge)preferredScreenEdgesDeferringSystemGestures
/* 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;
Expand All @@ -267,11 +271,12 @@ - (void)initKeyboard
textField.hidden = YES;
keyboardVisible = NO;

#if !TARGET_OS_TV
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
#if !TARGET_OS_TV
[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
Expand Down Expand Up @@ -310,11 +315,12 @@ - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceO

- (void)deinitKeyboard
{
#if !TARGET_OS_TV
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
#if !TARGET_OS_TV
[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 */
Expand Down Expand Up @@ -354,6 +360,50 @@ - (void)keyboardWillHide:(NSNotification *)notification
[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;
Expand Down Expand Up @@ -392,49 +442,20 @@ - (void)setKeyboardHeight:(int)height
- (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);
} 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);
}
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);
}

SDL_SendKeyboardText([string UTF8String]);
if (textField.text.length < 16) {
textField.text = obligateForBackspace;
}
} else {
changeText = string;
}

return NO; /* don't allow the edit! (keep placeholder text there) */
return YES;
}

/* Terminates the editing session */
Expand Down Expand Up @@ -498,7 +519,7 @@ - (BOOL)textFieldShouldReturn:(UITextField*)_textField
@autoreleasepool {
SDL_uikitviewcontroller *vc = GetWindowViewController(window);
if (vc != nil) {
return vc.isKeyboardVisible;
return vc.keyboardVisible;
}
return SDL_FALSE;
}
Expand Down

0 comments on commit 0b3a350

Please sign in to comment.