Define CustomTapGestureRecognizer to reliably toggle local keyboard, better than UITapGestureRecognizer

This commit is contained in:
True Zhuanjia
2024-05-15 16:56:47 +08:00
parent 208688afe1
commit b1b0be886e
4 changed files with 71 additions and 5 deletions
-1
View File
@@ -86,7 +86,6 @@
self.absoluteTouchMode = settings.absoluteTouchMode;
self.statsOverlay = settings.statsOverlay;
self.keyboardToggleTaps = @1;
self.keyboardToggleFingers = @4;
#endif
self.uniqueId = settings.uniqueId;
@@ -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 */
@@ -0,0 +1,40 @@
//
// CustomTapGestureRecognizer.m
// Moonlight
//
// Created by Admin on 2024/5/15.
// Copyright © 2024 Moonlight Game Streaming Project. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIGestureRecognizerSubclass.h>
#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<UITouch *> *)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<UITouch *> *)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
+11 -4
View File
@@ -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<NSString *, NSNumber *> *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];
}
}