-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShortcutManager.swift
104 lines (84 loc) · 3.41 KB
/
ShortcutManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import Foundation
import UIKit
// Hack: To look into keyboard events
fileprivate extension UIEvent {
fileprivate var isKeyboardEvent: Bool { String(describing: Swift.type(of: self)) == "UIPhysicalKeyboardEvent" }
fileprivate var modifiedInput: String? {
guard isKeyboardEvent else { return nil }
return self.value(forKey: "_modifiedInput") as? String
}
fileprivate var unmodifiedInput: String? {
guard isKeyboardEvent else { return nil }
return self.value(forKey: "_unmodifiedInput") as? String
}
fileprivate var isKeyDown: Bool? {
guard isKeyboardEvent else { return nil }
return (self.value(forKey: "_isKeyDown") as? Bool)
}
fileprivate var keyCode: Int? {
guard isKeyboardEvent else { return nil }
return (self.value(forKey: "_keyCode") as? Int)
}
}
class ShortcutManager {
private var actionsForKeyInputs: [String: () -> Void] = [:]
static let initializeOnce = {
performSwizzling()
}()
public static let sharedInstance = {
ShortcutManager.initializeOnce
return ShortcutManager()
}()
// Registers a action to be performed when "key" is presseed by user
// Note: This will override existing action for the "key"
public func registerShortcut(withKey key: String, action: @escaping () -> Void) {
actionsForKeyInputs[key] = action
}
private func handleKeyboardEvent(pressedKey: String) {
if let action = actionsForKeyInputs[pressedKey] {
action()
}
}
fileprivate func interceptedSendEvent(_ event: UIEvent) {
guard event.isKeyboardEvent, event.isKeyDown ?? false else { return }
if let input = event.modifiedInput {
handleKeyboardEvent(pressedKey: input)
}
}
}
// Handle siwizzling: Just call `performSwizzling()`
// Expect call to `ShortcutManager.interceptedSendEvent(_: UIEvent)` when any event is performed on `UIApplication`.
private extension ShortcutManager {
// Performs swizzle only once
private static func performSwizzling() {
_ = _swizzledOnce
}
private static var _swizzledOnce: () = {
_swizzle()
}()
private static func _swizzle() {
let originalSelector = #selector(UIApplication.sendEvent(_:))
let swizzledSelector = #selector(UIApplication.swizzled_sendEvent(_:))
guard let originalMethod = class_getInstanceMethod(UIApplication.self, originalSelector),
let swizzledMethod = class_getInstanceMethod(UIApplication.self, swizzledSelector) else {
return
}
let didAddMethod = class_addMethod(UIApplication.self, originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod))
if didAddMethod {
class_replaceMethod(UIApplication.self, swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod))
} else {
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
}
fileprivate extension UIApplication {
@objc func swizzled_sendEvent(_ event: UIEvent) {
ShortcutManager.sharedInstance.interceptedSendEvent(event)
// Call original
swizzled_sendEvent(event)
}
}