Improved iOS Bluetooth keyboard support
authorSam Lantinga
Wed, 22 May 2019 17:39:51 -0700
changeset 12750455b89ce7384
parent 12747 cdf53e16feb7
child 12751 06fa9a25d2a0
Improved iOS Bluetooth keyboard support
* Don't stop text input after the return key is pressed
* Handle arrow and escape keys
src/video/uikit/SDL_uikitviewcontroller.m
     1.1 --- a/src/video/uikit/SDL_uikitviewcontroller.m	Tue May 21 17:33:31 2019 -0700
     1.2 +++ b/src/video/uikit/SDL_uikitviewcontroller.m	Wed May 22 17:39:51 2019 -0700
     1.3 @@ -73,6 +73,7 @@
     1.4  
     1.5  #if SDL_IPHONE_KEYBOARD
     1.6      UITextField *textField;
     1.7 +    BOOL hardwareKeyboard;
     1.8      BOOL showingKeyboard;
     1.9      BOOL rotatingOrientation;
    1.10      NSString *changeText;
    1.11 @@ -89,7 +90,9 @@
    1.12  
    1.13  #if SDL_IPHONE_KEYBOARD
    1.14          [self initKeyboard];
    1.15 -        rotatingOrientation = FALSE;
    1.16 +        hardwareKeyboard = NO;
    1.17 +        showingKeyboard = NO;
    1.18 +        rotatingOrientation = NO;
    1.19  #endif
    1.20  
    1.21  #if TARGET_OS_TV
    1.22 @@ -280,6 +283,41 @@
    1.23      [center addObserver:self selector:@selector(textFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
    1.24  }
    1.25  
    1.26 +- (NSArray *) keyCommands {
    1.27 +    NSMutableArray *commands = [[NSMutableArray alloc] init];
    1.28 +    [commands addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputUpArrow modifierFlags:kNilOptions action:@selector(handleCommand:)]];
    1.29 +    [commands addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputDownArrow modifierFlags:kNilOptions action:@selector(handleCommand:)]];
    1.30 +    [commands addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputLeftArrow modifierFlags:kNilOptions action:@selector(handleCommand:)]];
    1.31 +    [commands addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputRightArrow modifierFlags:kNilOptions action:@selector(handleCommand:)]];
    1.32 +    [commands addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputEscape modifierFlags:kNilOptions action:@selector(handleCommand:)]];
    1.33 +    return [NSArray arrayWithArray:commands];
    1.34 +}
    1.35 +
    1.36 +- (void) handleCommand: (UIKeyCommand *) keyCommand {
    1.37 +    SDL_Scancode scancode = SDL_SCANCODE_UNKNOWN;
    1.38 +
    1.39 +    if (keyCommand.input == UIKeyInputUpArrow) {
    1.40 +        scancode = SDL_SCANCODE_UP;
    1.41 +    } else if (keyCommand.input == UIKeyInputDownArrow) {
    1.42 +        scancode = SDL_SCANCODE_DOWN;
    1.43 +    } else if (keyCommand.input == UIKeyInputLeftArrow) {
    1.44 +        scancode = SDL_SCANCODE_LEFT;
    1.45 +    } else if (keyCommand.input == UIKeyInputRightArrow) {
    1.46 +        scancode = SDL_SCANCODE_RIGHT;
    1.47 +    } else if (keyCommand.input == UIKeyInputEscape) {
    1.48 +        scancode = SDL_SCANCODE_ESCAPE;
    1.49 +    }
    1.50 +
    1.51 +    if (scancode != SDL_SCANCODE_UNKNOWN) {
    1.52 +        SDL_SendKeyboardKey(SDL_PRESSED, scancode);
    1.53 +        SDL_SendKeyboardKey(SDL_RELEASED, scancode);
    1.54 +    }
    1.55 +}
    1.56 +
    1.57 +- (void) downArrow: (UIKeyCommand *) keyCommand {
    1.58 +    NSLog(@"down arrow!");
    1.59 +}
    1.60 +
    1.61  - (void)setView:(UIView *)view
    1.62  {
    1.63      [super setView:view];
    1.64 @@ -296,21 +334,21 @@
    1.65  - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
    1.66  {
    1.67      [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    1.68 -    rotatingOrientation = TRUE;
    1.69 +    rotatingOrientation = YES;
    1.70      [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {}
    1.71                                   completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
    1.72 -                                     rotatingOrientation = FALSE;
    1.73 +                                     rotatingOrientation = NO;
    1.74                                   }];
    1.75  }
    1.76  #else
    1.77  - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    1.78      [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    1.79 -    rotatingOrientation = TRUE;
    1.80 +    rotatingOrientation = YES;
    1.81  }
    1.82  
    1.83  - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    1.84      [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
    1.85 -    rotatingOrientation = FALSE;
    1.86 +    rotatingOrientation = NO;
    1.87  }
    1.88  #endif /* TARGET_OS_TV || __IPHONE_OS_VERSION_MIN_REQUIRED >= 80000 */
    1.89  
    1.90 @@ -466,7 +504,8 @@
    1.91  {
    1.92      SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_RETURN);
    1.93      SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_RETURN);
    1.94 -    if (SDL_GetHintBoolean(SDL_HINT_RETURN_KEY_HIDES_IME, SDL_FALSE)) {
    1.95 +    if (keyboardVisible &&
    1.96 +        SDL_GetHintBoolean(SDL_HINT_RETURN_KEY_HIDES_IME, SDL_FALSE)) {
    1.97           SDL_StopTextInput();
    1.98      }
    1.99      return YES;