From 5037d8840a28fd0db0a38b42e8b3b0651ec64e9e Mon Sep 17 00:00:00 2001 From: Silence <374619540@qq.com> Date: Wed, 5 Apr 2017 15:04:10 +0800 Subject: [PATCH] Update --- Category.xcodeproj/project.pbxproj | 6 - Classes/Manager/SIKeyboardShortcutManager.h | 40 --- Classes/Manager/SIKeyboardShortcutManager.m | 278 -------------------- Classes/Manager/SIManager.h | 1 - 4 files changed, 325 deletions(-) delete mode 100644 Classes/Manager/SIKeyboardShortcutManager.h delete mode 100644 Classes/Manager/SIKeyboardShortcutManager.m diff --git a/Category.xcodeproj/project.pbxproj b/Category.xcodeproj/project.pbxproj index c0005b2..8346b37 100644 --- a/Category.xcodeproj/project.pbxproj +++ b/Category.xcodeproj/project.pbxproj @@ -227,7 +227,6 @@ E8FB93811E606D5C00B77407 /* SIIAppPurchaseManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E8FB93801E606D5C00B77407 /* SIIAppPurchaseManager.m */; }; E8FB93841E607CE700B77407 /* SIAppVersionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E8FB93831E607CE700B77407 /* SIAppVersionManager.m */; }; E8FF5EB01E5B244F00859425 /* SIAuthorizationManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E8FF5EAF1E5B244F00859425 /* SIAuthorizationManager.m */; }; - E8FF5EB31E5B246600859425 /* SIKeyboardShortcutManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E8FF5EB21E5B246600859425 /* SIKeyboardShortcutManager.m */; }; E8FF5EB61E5B250200859425 /* SITouchIDManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E8FF5EB51E5B250200859425 /* SITouchIDManager.m */; }; E8FF5EB91E5B342500859425 /* SIBluetoothManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E8FF5EB81E5B342500859425 /* SIBluetoothManager.m */; }; /* End PBXBuildFile section */ @@ -721,8 +720,6 @@ E8FB93851E6080C100B77407 /* SIManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SIManager.h; sourceTree = ""; }; E8FF5EAE1E5B244F00859425 /* SIAuthorizationManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SIAuthorizationManager.h; sourceTree = ""; }; E8FF5EAF1E5B244F00859425 /* SIAuthorizationManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SIAuthorizationManager.m; sourceTree = ""; }; - E8FF5EB11E5B246600859425 /* SIKeyboardShortcutManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SIKeyboardShortcutManager.h; sourceTree = ""; }; - E8FF5EB21E5B246600859425 /* SIKeyboardShortcutManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SIKeyboardShortcutManager.m; sourceTree = ""; }; E8FF5EB41E5B250200859425 /* SITouchIDManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SITouchIDManager.h; sourceTree = ""; }; E8FF5EB51E5B250200859425 /* SITouchIDManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SITouchIDManager.m; sourceTree = ""; }; E8FF5EB71E5B342500859425 /* SIBluetoothManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SIBluetoothManager.h; sourceTree = ""; }; @@ -1779,8 +1776,6 @@ isa = PBXGroup; children = ( E8FB93851E6080C100B77407 /* SIManager.h */, - E8FF5EB11E5B246600859425 /* SIKeyboardShortcutManager.h */, - E8FF5EB21E5B246600859425 /* SIKeyboardShortcutManager.m */, E8FF5EAE1E5B244F00859425 /* SIAuthorizationManager.h */, E8FF5EAF1E5B244F00859425 /* SIAuthorizationManager.m */, E8FF5EB41E5B250200859425 /* SITouchIDManager.h */, @@ -1948,7 +1943,6 @@ E8E946661E210B6D00EE98B0 /* UIViewController+Alert.m in Sources */, E8E946181E210B6D00EE98B0 /* NSNumber+Round.m in Sources */, E8E946031E210B6D00EE98B0 /* NSBundle+AppInfo.m in Sources */, - E8FF5EB31E5B246600859425 /* SIKeyboardShortcutManager.m in Sources */, E8E946461E210B6D00EE98B0 /* UIGestureRecognizer+ALActionBlocks.m in Sources */, E83465E91E8ABA2600F1F757 /* UINavigationItem+Loading.m in Sources */, E83465D91E8AB20000F1F757 /* UIView+draggable.m in Sources */, diff --git a/Classes/Manager/SIKeyboardShortcutManager.h b/Classes/Manager/SIKeyboardShortcutManager.h deleted file mode 100644 index c5e56ca..0000000 --- a/Classes/Manager/SIKeyboardShortcutManager.h +++ /dev/null @@ -1,40 +0,0 @@ -// -// SIKeyboardShortcutManager.h -// KeyboardShortcutManagerDemo -// -// Created by 杨晴贺 on 19/02/2017. -// Copyright © 2017 Silence. All rights reserved. -// - -#import - -typedef void(^KeyBoardAction)(); - -#if TARGET_OS_SIMULATOR - -@interface SIKeyboardShortcutManager : NSObject - -@property (nonatomic,assign,getter=isEnabled) BOOL enabled ; /// 是否开启,默认是开启 - - -/** - 单例 - - @return Manager单例 - */ -+ (instancetype)sharedManager ; - - -/** - 注册快捷键对应的Action - - @param key 键字符串 - @param modifiers 控制键类型 - @param action 行为Action - */ -- (void)registerSimulatorShortcutWithKey:(NSString *)key - modifiers:(UIKeyModifierFlags)modifiers - action:(KeyBoardAction)action; - -@end -#endif diff --git a/Classes/Manager/SIKeyboardShortcutManager.m b/Classes/Manager/SIKeyboardShortcutManager.m deleted file mode 100644 index 880a783..0000000 --- a/Classes/Manager/SIKeyboardShortcutManager.m +++ /dev/null @@ -1,278 +0,0 @@ -// -// SIKeyboardShortcutManager.m -// KeyboardShortcutManagerDemo -// -// Created by 杨晴贺 on 19/02/2017. -// Copyright © 2017 Silence. All rights reserved. -// - -#import "SIKeyboardShortcutManager.h" -#import - -// https://github.com/silence0201/Headers/blob/ab28eba3cd027298bc4689972179226e1440148a/iOS10/Frameworks/UIKit/UIPhysicalKeyboardEvent.h - -#if TARGET_OS_SIMULATOR -@interface UIEvent (UIPhysicalKeyboardEvent) - -@property (nonatomic,strong) NSString *_modifiedInput; -@property (nonatomic,strong) NSString *_unmodifiedInput; -@property (nonatomic,assign) UIKeyModifierFlags _modifierFlags; -@property (nonatomic,assign) BOOL _isKeyDown; -@property (nonatomic,assign) long _keyCode; - -@end - -@interface _KeyInput : NSObject - -@property (nonatomic,copy,readonly) NSString *key; -@property (nonatomic,assign,readonly) UIKeyModifierFlags flags; - -@end - -@implementation _KeyInput - -- (BOOL)isEqual:(id)object{ - BOOL isEqual = NO; - if ([object isKindOfClass:[_KeyInput class]]) { - _KeyInput *keyCommand = (_KeyInput *)object; - BOOL equalKeys = self.key == keyCommand.key || [self.key isEqual:keyCommand.key]; - BOOL equalFlags = self.flags == keyCommand.flags; - isEqual = equalKeys && equalFlags; - } - return isEqual; -} - -- (NSUInteger)hash{ - return [self.key hash] ^ self.flags; -} - -- (instancetype)copyWithZone:(NSZone *)zone{ - return [[self class] keyInputForKey:self.key flags:self.flags]; -} - - -+ (instancetype)keyInputForKey:(NSString *)key flags:(UIKeyModifierFlags)flags{ - _KeyInput *keyInput = [[self alloc] init]; - if (keyInput) { - keyInput->_key = key; - keyInput->_flags = flags; - } - return keyInput; -} - -@end - -static const long kSIControlKeyCode = 0xe0; -static const long kSIShiftKeyCode = 0xe1; -static const long kSICommandKeyCode = 0xe3; - -@interface SIKeyboardShortcutManager () - -@property (nonatomic,strong) NSMutableDictionary *actionsForKeyInput ; - -@property (nonatomic,assign,getter=isPressingShift) BOOL pressingShift ; -@property (nonatomic,assign,getter=isPressingCommand) BOOL pressingCommand ; -@property (nonatomic,assign,getter=isPressingControl) BOOL pressingControl ; - -@end - -@implementation SIKeyboardShortcutManager - -// 加载时交换方法 -+ (void)load{ - SEL originalKeyEventSelector = NSSelectorFromString(@"handleKeyUIEvent:"); - SEL swizzledKeyEventSelector = [SIKeyboardShortcutManager swizzledSelectorForSelector:originalKeyEventSelector]; - - void (^handleKeyUIEventSwizzleBlock)(UIApplication *, UIEvent *) = ^(UIApplication *slf, UIEvent *event) { - [[[self class] sharedManager] handleKeyboardEvent:event]; - ((void(*)(id, SEL, id))objc_msgSend)(slf, swizzledKeyEventSelector, event); - }; - - [SIKeyboardShortcutManager replaceImplementationOfKnownSelector:originalKeyEventSelector - onClass:[UIApplication class] - withBlock:handleKeyUIEventSwizzleBlock - swizzledSelector:swizzledKeyEventSelector]; - - if ([[UITouch class] instancesRespondToSelector:@selector(maximumPossibleForce)]) { - SEL originalSendEventSelector = NSSelectorFromString(@"sendEvent:"); - SEL swizzledSendEventSelector = [SIKeyboardShortcutManager swizzledSelectorForSelector:originalSendEventSelector]; - - void (^sendEventSwizzleBlock)(UIApplication *, UIEvent *) = ^(UIApplication *slf, UIEvent *event) { - if (event.type == UIEventTypeTouches) { - SIKeyboardShortcutManager *keyboardManager = [SIKeyboardShortcutManager sharedManager]; - NSInteger pressureLevel = 0; - if (keyboardManager.isPressingShift) { - pressureLevel++; - } - if (keyboardManager.isPressingCommand) { - pressureLevel++; - } - if (keyboardManager.isPressingControl) { - pressureLevel++; - } - if (pressureLevel > 0) { - for (UITouch *touch in [event allTouches]) { - double adjustedPressureLevel = pressureLevel * 20 * touch.maximumPossibleForce; - [touch setValue:@(adjustedPressureLevel) forKey:@"_pressure"]; - } - } - } - - ((void(*)(id, SEL, id))objc_msgSend)(slf, swizzledSendEventSelector, event); - }; - - [SIKeyboardShortcutManager replaceImplementationOfKnownSelector:originalSendEventSelector - onClass:[UIApplication class] - withBlock:sendEventSwizzleBlock - swizzledSelector:swizzledSendEventSelector]; - - SEL originalSupportsTouchPressureSelector = NSSelectorFromString(@"_supportsForceTouch"); - SEL swizzledSupportsTouchPressureSelector = [SIKeyboardShortcutManager swizzledSelectorForSelector:originalSupportsTouchPressureSelector]; - - BOOL (^supportsTouchPressureSwizzleBlock)(UIDevice *) = ^BOOL(UIDevice *slf) { - return YES; - }; - - [SIKeyboardShortcutManager replaceImplementationOfKnownSelector:originalSupportsTouchPressureSelector - onClass:[UIDevice class] - withBlock:supportsTouchPressureSwizzleBlock - swizzledSelector:swizzledSupportsTouchPressureSelector]; - } -} - - -+ (instancetype)sharedManager{ - static SIKeyboardShortcutManager *sharedManager = nil ; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - sharedManager = [[[self class] alloc] init] ; - }); - return sharedManager ; -} - -- (instancetype)init{ - if(self = [super init]){ - _actionsForKeyInput = [NSMutableDictionary dictionary] ; - _enabled = YES ; - } - return self ; -} - -- (void)registerSimulatorShortcutWithKey:(NSString *)key modifiers:(UIKeyModifierFlags)modifiers action:(KeyBoardAction)action{ - _KeyInput *keyInput = [_KeyInput keyInputForKey:key flags:modifiers] ; - [self.actionsForKeyInput setObject:action forKey:keyInput] ; -} - -- (void)handleKeyboardEvent:(UIEvent *)event{ - if (!self.enabled) return ; - - NSString *modifiedInput = nil; - NSString *unmodifiedInput = nil; - UIKeyModifierFlags flags = 0; - BOOL isKeyDown = NO; - - if ([event respondsToSelector:@selector(_modifiedInput)]) { - modifiedInput = [event _modifiedInput] ; - } - - if ([event respondsToSelector:@selector(_unmodifiedInput)]){ - unmodifiedInput = [event _unmodifiedInput] ; - } - - if ([event respondsToSelector:@selector(_modifierFlags)]){ - flags = [event _modifierFlags] ; - } - - if ([event respondsToSelector:@selector(_isKeyDown)]){ - isKeyDown = [event _isKeyDown] ; - } - - BOOL interactionEnabled = ![[UIApplication sharedApplication] isIgnoringInteractionEvents]; - BOOL hasFirstResponder = NO; - if (isKeyDown && [modifiedInput length] > 0 && interactionEnabled) { - UIResponder *firstResponder = nil; - for (UIWindow *window in [SIKeyboardShortcutManager allWindows]) { - firstResponder = [window valueForKey:@"firstResponder"]; - if (firstResponder) { - hasFirstResponder = YES; - break; - } - } - - if (firstResponder) { - if ([unmodifiedInput isEqual:UIKeyInputEscape]) { - [firstResponder resignFirstResponder]; - } - } else { - _KeyInput *exactMatch = [_KeyInput keyInputForKey:unmodifiedInput flags:flags]; - KeyBoardAction actionBlock = self.actionsForKeyInput[exactMatch]; - - if (!actionBlock) { - _KeyInput *shiftMatch = [_KeyInput keyInputForKey:modifiedInput flags:flags&(!UIKeyModifierShift)]; - actionBlock = self.actionsForKeyInput[shiftMatch]; - } - - if (!actionBlock) { - _KeyInput *capitalMatch = [_KeyInput keyInputForKey:[unmodifiedInput uppercaseString] flags:flags]; - actionBlock = self.actionsForKeyInput[capitalMatch]; - } - - if (actionBlock) { - actionBlock(); - } - } - } - - if (!hasFirstResponder && [event respondsToSelector:@selector(_keyCode)]) { - long keyCode = [event _keyCode]; - if (keyCode == kSIControlKeyCode) { - self.pressingControl = isKeyDown; - } else if (keyCode == kSICommandKeyCode) { - self.pressingCommand = isKeyDown; - } else if (keyCode == kSIShiftKeyCode) { - self.pressingShift = isKeyDown; - } - } -} - -#pragma mark Utils - -+ (NSArray *)allWindows{ - BOOL includeInternalWindows = YES; - BOOL onlyVisibleWindows = NO; - - NSArray *allWindowsComponents = @[@"al", @"lWindo", @"wsIncl", @"udingInt", @"ernalWin", @"dows:o", @"nlyVisi", @"bleWin", @"dows:"]; - SEL allWindowsSelector = NSSelectorFromString([allWindowsComponents componentsJoinedByString:@""]); - - NSMethodSignature *methodSignature = [[UIWindow class] methodSignatureForSelector:allWindowsSelector]; - NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature]; - - invocation.target = [UIWindow class]; - invocation.selector = allWindowsSelector; - [invocation setArgument:&includeInternalWindows atIndex:2]; - [invocation setArgument:&onlyVisibleWindows atIndex:3]; - [invocation invoke]; - - __unsafe_unretained NSArray *windows = nil; - [invocation getReturnValue:&windows]; - return windows; -} - -+ (SEL)swizzledSelectorForSelector:(SEL)selector{ - return NSSelectorFromString([NSString stringWithFormat:@"_si_swizzle_%x_%@", arc4random(), NSStringFromSelector(selector)]); -} - -+ (void)replaceImplementationOfKnownSelector:(SEL)originalSelector onClass:(Class)class withBlock:(id)block swizzledSelector:(SEL)swizzledSelector{ - Method originalMethod = class_getInstanceMethod(class, originalSelector); - if (!originalMethod) { - return; - } - - IMP implementation = imp_implementationWithBlock(block); - class_addMethod(class, swizzledSelector, implementation, method_getTypeEncoding(originalMethod)); - Method newMethod = class_getInstanceMethod(class, swizzledSelector); - method_exchangeImplementations(originalMethod, newMethod); -} - -@end -#endif diff --git a/Classes/Manager/SIManager.h b/Classes/Manager/SIManager.h index e12aace..9884fa5 100644 --- a/Classes/Manager/SIManager.h +++ b/Classes/Manager/SIManager.h @@ -7,7 +7,6 @@ // -#import "SIKeyboardShortcutManager.h" #import "SIAuthorizationManager.h" #import "SIAppVersionManager.h" #import "SIKeyboardManager.h"