make key mapping static

This commit is contained in:
True Zhuanjia committed 2024-07-24 19:29:52 +08:00
1 parent a975b0e24a
commit b6ecdcd0ea
2 files changed
+6 -5

No files matched your search

+6 -4
View File
@@ -40,8 +40,7 @@ import UIKit
@objc public class CommandManager: NSObject { @objc public class CommandManager: NSObject {
@objc public static let shared = CommandManager() @objc public static let shared = CommandManager()
private var commands: [RemoteCommand] = [] static let keyMappings: [String: UInt16] = [
private let keyMappings: [String: UInt16] = [
"CTRL": 0x11, "CTRL": 0x11,
"ALT": 0x12, "ALT": 0x12,
"DEL": 0x2E, "DEL": 0x2E,
@@ -49,6 +48,8 @@ import UIKit
"F2": 0x71 "F2": 0x71
] ]
private var commands: [RemoteCommand] = []
public weak var viewController: CommandManagerViewController? public weak var viewController: CommandManagerViewController?
private override init() { private override init() {
@@ -57,12 +58,12 @@ import UIKit
} }
@objc public func createTestKeyMappings() -> [String: UInt16] { @objc public func createTestKeyMappings() -> [String: UInt16] {
return keyMappings return CommandManager.keyMappings
} }
// extractKeyStrings from keyboardCMDString // extractKeyStrings from keyboardCMDString
@objc public func extractKeyStrings(from input: String) -> [String]? { @objc public func extractKeyStrings(from input: String) -> [String]? {
let keys = keyMappings.keys.joined(separator: "|") let keys = CommandManager.keyMappings.keys.joined(separator: "|")
let pattern = "^(?:(\(keys))(?:\\+(\(keys))*)*)$" let pattern = "^(?:(\(keys))(?:\\+(\(keys))*)*)$"
guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else { guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else {
@@ -78,6 +79,7 @@ import UIKit
let matchedString = (input as NSString).substring(with: match.range(at: 0)) let matchedString = (input as NSString).substring(with: match.range(at: 0))
let keyStrings = matchedString.split(separator: "+").map { String($0) } let keyStrings = matchedString.split(separator: "+").map { String($0) }
guard !keyStrings.isEmpty else { guard !keyStrings.isEmpty else {
print("No key strings found in the matched string") print("No key strings found in the matched string")
return nil return nil
@@ -199,7 +199,6 @@ import UIKit
if let indexPath = previouslySelectedIndexPath { if let indexPath = previouslySelectedIndexPath {
// Make sure the indexPath is still valid and scroll to the selected indexPath // Make sure the indexPath is still valid and scroll to the selected indexPath
if indexPath.row < tableView.numberOfRows(inSection: indexPath.section) { if indexPath.row < tableView.numberOfRows(inSection: indexPath.section) {
print("scrollToRow")
tableView.selectRow(at: indexPath, animated: true, scrollPosition: .middle) // keep the entry of previous index selected. tableView.selectRow(at: indexPath, animated: true, scrollPosition: .middle) // keep the entry of previous index selected.
} }
} }