From 11ec500dff8ad0e5584e010533d4850a033e7c90 Mon Sep 17 00:00:00 2001 From: Andrey Filipenkov Date: Sun, 22 Jan 2023 15:34:16 +0300 Subject: [PATCH] [iOS] use pinch gesture as Spacebar hotkey instead of Tab --- client/ios/GameChatKeyboardHandler.h | 4 +++- client/ios/GameChatKeyboardHandler.m | 36 ++++++++++++++-------------- client/ios/startSDL.mm | 2 +- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/client/ios/GameChatKeyboardHandler.h b/client/ios/GameChatKeyboardHandler.h index 01d8fd694..6d8f23c87 100644 --- a/client/ios/GameChatKeyboardHandler.h +++ b/client/ios/GameChatKeyboardHandler.h @@ -10,11 +10,13 @@ #import +#include + NS_ASSUME_NONNULL_BEGIN @interface GameChatKeyboardHandler : NSObject -- (void)triggerInput; ++ (void)sendKeyEventWithKeyCode:(SDL_KeyCode)keyCode; @end diff --git a/client/ios/GameChatKeyboardHandler.m b/client/ios/GameChatKeyboardHandler.m index 276ece8bb..4b1143caf 100644 --- a/client/ios/GameChatKeyboardHandler.m +++ b/client/ios/GameChatKeyboardHandler.m @@ -10,20 +10,8 @@ #import "GameChatKeyboardHandler.h" -#include - static int watchReturnKey(void * userdata, SDL_Event * event); -static void sendKeyEvent(SDL_KeyCode keyCode) -{ - SDL_Event keyEvent; - keyEvent.key = (SDL_KeyboardEvent){ - .type = SDL_KEYDOWN, - .keysym.sym = keyCode, - }; - SDL_PushEvent(&keyEvent); -} - @interface GameChatKeyboardHandler () @property (nonatomic) BOOL wasChatMessageSent; @@ -31,28 +19,40 @@ static void sendKeyEvent(SDL_KeyCode keyCode) @implementation GameChatKeyboardHandler -- (void)triggerInput { ++ (void)sendKeyEventWithKeyCode:(SDL_KeyCode)keyCode +{ + SDL_Event keyEvent; + keyEvent.key = (SDL_KeyboardEvent){ + .type = SDL_KEYDOWN, + .state = SDL_PRESSED, + .keysym.sym = keyCode, + }; + SDL_PushEvent(&keyEvent); +} + +- (instancetype)init { + self = [super init]; + __auto_type notificationCenter = NSNotificationCenter.defaultCenter; [notificationCenter addObserver:self selector:@selector(textDidBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil]; [notificationCenter addObserver:self selector:@selector(textDidEndEditing:) name:UITextFieldTextDidEndEditingNotification object:nil]; - self.wasChatMessageSent = NO; - sendKeyEvent(SDLK_TAB); + return self; } #pragma mark - Notifications - (void)textDidBeginEditing:(NSNotification *)n { + self.wasChatMessageSent = NO; + // watch for pressing Return to ignore sending Escape key after keyboard is closed SDL_AddEventWatch(watchReturnKey, (__bridge void *)self); } - (void)textDidEndEditing:(NSNotification *)n { - [NSNotificationCenter.defaultCenter removeObserver:self]; - // discard chat message if(!self.wasChatMessageSent) - sendKeyEvent(SDLK_ESCAPE); + [[self class] sendKeyEventWithKeyCode:SDLK_ESCAPE]; } @end diff --git a/client/ios/startSDL.mm b/client/ios/startSDL.mm index c9ada242b..ce2dbd0a6 100644 --- a/client/ios/startSDL.mm +++ b/client/ios/startSDL.mm @@ -95,7 +95,7 @@ - (void)handlePinch:(UIGestureRecognizer *)gesture { if(gesture.state != UIGestureRecognizerStateBegan || CSH->state != EClientState::GAMEPLAY) return; - [self.gameChatHandler triggerInput]; + [GameChatKeyboardHandler sendKeyEventWithKeyCode:SDLK_SPACE]; } #pragma mark - UIGestureRecognizerDelegate