From b1b0be886ed738655efedc944edb916a730f63cf Mon Sep 17 00:00:00 2001 From: True Zhuanjia Date: Wed, 15 May 2024 16:56:47 +0800 Subject: [PATCH] Define CustomTapGestureRecognizer to reliably toggle local keyboard, better than UITapGestureRecognizer --- Limelight/Database/TemporarySettings.m | 1 - Limelight/Input/CustomTapGestureRecognizer.h | 20 ++++++++++ Limelight/Input/CustomTapGestureRecognizer.m | 40 ++++++++++++++++++++ Limelight/Input/StreamView.m | 15 ++++++-- 4 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 Limelight/Input/CustomTapGestureRecognizer.h create mode 100644 Limelight/Input/CustomTapGestureRecognizer.m diff --git a/Limelight/Database/TemporarySettings.m b/Limelight/Database/TemporarySettings.m index 66bd48f0..a2ae8561 100644 --- a/Limelight/Database/TemporarySettings.m +++ b/Limelight/Database/TemporarySettings.m @@ -86,7 +86,6 @@ self.absoluteTouchMode = settings.absoluteTouchMode; self.statsOverlay = settings.statsOverlay; - self.keyboardToggleTaps = @1; self.keyboardToggleFingers = @4; #endif self.uniqueId = settings.uniqueId; diff --git a/Limelight/Input/CustomTapGestureRecognizer.h b/Limelight/Input/CustomTapGestureRecognizer.h new file mode 100644 index 00000000..a4e8248b --- /dev/null +++ b/Limelight/Input/CustomTapGestureRecognizer.h @@ -0,0 +1,20 @@ +// +// CustomTapGestureRecognizer.h +// Moonlight +// +// Created by Admin on 2024/5/15. +// Copyright © 2024 Moonlight Game Streaming Project. All rights reserved. +// + +#ifndef CustomTapGestureRecognizer_h +#define CustomTapGestureRecognizer_h + + + +@interface CustomTapGestureRecognizer : UIGestureRecognizer + +@property (nonatomic, assign) uint8_t numberOfTouchesRequired; +@property (nonatomic, assign) double tapDownTimeThreshold; + +@end +#endif /* CustomTapGestureRecognizer_h */ diff --git a/Limelight/Input/CustomTapGestureRecognizer.m b/Limelight/Input/CustomTapGestureRecognizer.m new file mode 100644 index 00000000..91493921 --- /dev/null +++ b/Limelight/Input/CustomTapGestureRecognizer.m @@ -0,0 +1,40 @@ +// +// CustomTapGestureRecognizer.m +// Moonlight +// +// Created by Admin on 2024/5/15. +// Copyright © 2024 Moonlight Game Streaming Project. All rights reserved. +// + +#import +#import +#import "CustomTapGestureRecognizer.h" + +@implementation CustomTapGestureRecognizer + +static NSTimeInterval multiFingerDownTime; +static bool multiFingerDown; + +- (instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action { + self = [super initWithTarget:target action:action]; + return self; +} + +- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { + // Check if the number of touches and taps meets the required criteria + if ([[event allTouches] count] == _numberOfTouchesRequired) { + multiFingerDownTime = CACurrentMediaTime(); + multiFingerDown = true; + self.state = UIGestureRecognizerStatePossible; + } +} + +- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { + // [super touchesEnded:touches withEvent:event]; + if(multiFingerDown && [[event allTouches] count] == [touches count]){ + multiFingerDown = false; + if((CACurrentMediaTime() - multiFingerDownTime) < _tapDownTimeThreshold / 1000) self.state = UIGestureRecognizerStateRecognized; + } +} + +@end diff --git a/Limelight/Input/StreamView.m b/Limelight/Input/StreamView.m index ea4848b4..6744f12d 100644 --- a/Limelight/Input/StreamView.m +++ b/Limelight/Input/StreamView.m @@ -15,6 +15,8 @@ #import "RelativeTouchHandler.h" #import "AbsoluteTouchHandler.h" #import "KeyboardInputField.h" +#import "CustomTapGestureRecognizer.h" + static const double X1_MOUSE_SPEED_DIVISOR = 2.5; @@ -24,7 +26,6 @@ static const double X1_MOUSE_SPEED_DIVISOR = 2.5; KeyboardInputField* keyInputField; BOOL isInputingText; NSMutableSet* keysDown; - uint32_t keyboardToggleFingers; float streamAspectRatio; @@ -47,6 +48,7 @@ static const double X1_MOUSE_SPEED_DIVISOR = 2.5; BOOL hasUserInteracted; NSDictionary *dictCodes; + CustomTapGestureRecognizer *keyboardToggleRecognizer; } - (void) setupStreamView:(ControllerSupport*)controllerSupport @@ -67,8 +69,13 @@ static const double X1_MOUSE_SPEED_DIVISOR = 2.5; [keyInputField setAutocapitalizationType:UITextAutocapitalizationTypeNone]; [keyInputField setSpellCheckingType:UITextSpellCheckingTypeNo]; [self addSubview:keyInputField]; - keyboardToggleFingers = settings.keyboardToggleFingers.intValue; - //[self addGestureRecognizer:_keyboardToggleTapRecognizer]; + + keyboardToggleRecognizer = [[CustomTapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleKeyboard)]; + keyboardToggleRecognizer.numberOfTouchesRequired = settings.keyboardToggleFingers.intValue; + keyboardToggleRecognizer.tapDownTimeThreshold = 300.0; // tap down time threshold in milli seconds. + keyboardToggleRecognizer.delaysTouchesBegan = NO; + keyboardToggleRecognizer.delaysTouchesEnded = NO; + [self addGestureRecognizer:keyboardToggleRecognizer]; #if TARGET_OS_TV // tvOS requires RelativeTouchHandler to manage Apple Remote input @@ -480,7 +487,7 @@ static const double X1_MOUSE_SPEED_DIVISOR = 2.5; // is triggered. [touchHandler touchesBegan:touches withEvent:event]; - if ([[event allTouches] count] == keyboardToggleFingers) [self toggleKeyboard]; + // if ([[event allTouches] count] == keyboardToggleFingers) [self toggleKeyboard]; } }