Properly backspace over text that was entered when autocorrect updates text with the iPhone on-screen keyboard
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
diff --git a/Android.mk b/Android.mk
old mode 100644
new mode 100755
diff --git a/src/video/uikit/SDL_uikitviewcontroller.m b/src/video/uikit/SDL_uikitviewcontroller.m
index d0dd863..4aa2d28 100644
--- a/src/video/uikit/SDL_uikitviewcontroller.m
+++ b/src/video/uikit/SDL_uikitviewcontroller.m
@@ -75,7 +75,7 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o
BOOL hardwareKeyboard;
BOOL showingKeyboard;
BOOL rotatingOrientation;
- NSString *changeText;
+ NSString *committedText;
NSString *obligateForBackspace;
#endif
}
@@ -263,12 +263,12 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o
/* 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 = obligateForBackspace;
+ committedText = textField.text;
/* set UITextInputTrait properties, mostly to defaults */
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
@@ -408,21 +408,39 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o
- (void)textFieldTextDidChange:(NSNotification *)notification
{
- if (changeText!=nil && textField.markedTextRange == nil)
- {
- NSUInteger len = changeText.length;
- if (len > 0) {
+ if (textField.markedTextRange == nil) {
+ NSUInteger compareLength = SDL_min(textField.text.length, committedText.length);
+ NSUInteger matchLength;
+
+ /* Backspace over characters that are no longer in the string */
+ for (matchLength = 0; matchLength < compareLength; ++matchLength) {
+ if ([committedText characterAtIndex:matchLength] != [textField.text characterAtIndex:matchLength]) {
+ break;
+ }
+ }
+ if (matchLength < committedText.length) {
+ size_t deleteLength = SDL_utf8strlen([[committedText substringFromIndex:matchLength] UTF8String]);
+ while (deleteLength > 0) {
+ /* Send distinct down and up events for each backspace action */
+ SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_BACKSPACE);
+ SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_BACKSPACE);
+ --deleteLength;
+ }
+ }
+
+ if (matchLength < textField.text.length) {
+ NSString *pendingText = [textField.text substringFromIndex:matchLength];
if (!SDL_HardwareKeyboardKeyPressed()) {
/* 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++) {
- SDL_SendKeyboardUnicodeKey([changeText characterAtIndex:i]);
+ NSUInteger i;
+ for (i = 0; i < pendingText.length; i++) {
+ SDL_SendKeyboardUnicodeKey([pendingText characterAtIndex:i]);
}
}
- SDL_SendKeyboardText([changeText UTF8String]);
+ SDL_SendKeyboardText([pendingText UTF8String]);
}
- changeText = nil;
+ committedText = textField.text;
}
}
@@ -463,18 +481,11 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o
/* UITextFieldDelegate method. Invoked when user types something. */
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
- NSUInteger len = string.length;
- if (len == 0) {
- changeText = nil;
- if (textField.markedTextRange == nil) {
- /* it wants to replace text with nothing, ie a delete */
- SDL_SendKeyboardKeyAutoRelease(SDL_SCANCODE_BACKSPACE);
- }
+ if (textField.markedTextRange == nil) {
if (textField.text.length < 16) {
textField.text = obligateForBackspace;
+ committedText = textField.text;
}
- } else {
- changeText = string;
}
return YES;
}