Skip to content

Commit

Permalink
Improved iOS Bluetooth keyboard support
Browse files Browse the repository at this point in the history
* Don't stop text input after the return key is pressed
* Handle arrow and escape keys
  • Loading branch information
slouken committed May 23, 2019
1 parent 50aab19 commit 7ec514d
Showing 1 changed file with 45 additions and 6 deletions.
51 changes: 45 additions & 6 deletions src/video/uikit/SDL_uikitviewcontroller.m
Expand Up @@ -73,6 +73,7 @@ @implementation SDL_uikitviewcontroller {

#if SDL_IPHONE_KEYBOARD
UITextField *textField;
BOOL hardwareKeyboard;
BOOL showingKeyboard;
BOOL rotatingOrientation;
NSString *changeText;
Expand All @@ -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
Expand Down Expand Up @@ -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];
Expand All @@ -296,21 +334,21 @@ - (void)setView:(UIView *)view
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
rotatingOrientation = TRUE;
rotatingOrientation = YES;
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {}
completion:^(id<UIViewControllerTransitionCoordinatorContext> 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 */

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7ec514d

Please sign in to comment.