From 7ec514d48f14b329f381806d89d463220ee75ff6 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 22 May 2019 17:39:51 -0700 Subject: [PATCH] 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 | 51 ++++++++++++++++++++--- 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/src/video/uikit/SDL_uikitviewcontroller.m b/src/video/uikit/SDL_uikitviewcontroller.m index 0e43cb68c9bbd..e1f4cdc45c0e4 100644 --- a/src/video/uikit/SDL_uikitviewcontroller.m +++ b/src/video/uikit/SDL_uikitviewcontroller.m @@ -73,6 +73,7 @@ @implementation SDL_uikitviewcontroller { #if SDL_IPHONE_KEYBOARD UITextField *textField; + BOOL hardwareKeyboard; BOOL showingKeyboard; BOOL rotatingOrientation; NSString *changeText; @@ -89,7 +90,9 @@ - (instancetype)initWithSDLWindow:(SDL_Window *)_window #if SDL_IPHONE_KEYBOARD [self initKeyboard]; - rotatingOrientation = FALSE; + hardwareKeyboard = NO; + showingKeyboard = NO; + rotatingOrientation = NO; #endif #if TARGET_OS_TV @@ -280,6 +283,41 @@ - (void)initKeyboard [center addObserver:self selector:@selector(textFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:nil]; } +- (NSArray *) keyCommands { + NSMutableArray *commands = [[NSMutableArray alloc] init]; + [commands addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputUpArrow modifierFlags:kNilOptions action:@selector(handleCommand:)]]; + [commands addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputDownArrow modifierFlags:kNilOptions action:@selector(handleCommand:)]]; + [commands addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputLeftArrow modifierFlags:kNilOptions action:@selector(handleCommand:)]]; + [commands addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputRightArrow modifierFlags:kNilOptions action:@selector(handleCommand:)]]; + [commands addObject:[UIKeyCommand keyCommandWithInput:UIKeyInputEscape modifierFlags:kNilOptions action:@selector(handleCommand:)]]; + return [NSArray arrayWithArray:commands]; +} + +- (void) handleCommand: (UIKeyCommand *) keyCommand { + SDL_Scancode scancode = SDL_SCANCODE_UNKNOWN; + + if (keyCommand.input == UIKeyInputUpArrow) { + scancode = SDL_SCANCODE_UP; + } else if (keyCommand.input == UIKeyInputDownArrow) { + scancode = SDL_SCANCODE_DOWN; + } else if (keyCommand.input == UIKeyInputLeftArrow) { + scancode = SDL_SCANCODE_LEFT; + } else if (keyCommand.input == UIKeyInputRightArrow) { + scancode = SDL_SCANCODE_RIGHT; + } else if (keyCommand.input == UIKeyInputEscape) { + scancode = SDL_SCANCODE_ESCAPE; + } + + if (scancode != SDL_SCANCODE_UNKNOWN) { + SDL_SendKeyboardKey(SDL_PRESSED, scancode); + SDL_SendKeyboardKey(SDL_RELEASED, scancode); + } +} + +- (void) downArrow: (UIKeyCommand *) keyCommand { + NSLog(@"down arrow!"); +} + - (void)setView:(UIView *)view { [super setView:view]; @@ -296,21 +334,21 @@ - (void)setView:(UIView *)view - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator { [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; - rotatingOrientation = TRUE; + rotatingOrientation = YES; [coordinator animateAlongsideTransition:^(id context) {} completion:^(id context) { - rotatingOrientation = FALSE; + rotatingOrientation = NO; }]; } #else - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; - rotatingOrientation = TRUE; + rotatingOrientation = YES; } - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; - rotatingOrientation = FALSE; + rotatingOrientation = NO; } #endif /* TARGET_OS_TV || __IPHONE_OS_VERSION_MIN_REQUIRED >= 80000 */ @@ -466,7 +504,8 @@ - (BOOL)textFieldShouldReturn:(UITextField*)_textField { SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_RETURN); SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_RETURN); - if (SDL_GetHintBoolean(SDL_HINT_RETURN_KEY_HIDES_IME, SDL_FALSE)) { + if (keyboardVisible && + SDL_GetHintBoolean(SDL_HINT_RETURN_KEY_HIDES_IME, SDL_FALSE)) { SDL_StopTextInput(); } return YES;