fix: potential issues with key repeats due to concurrency
This commit is contained in:
1 parent
3c4aaf5bba
commit
4cfe16a708
1 file changed
+14
-9
@@ -27,19 +27,24 @@ class KeyRepeatTimer {
|
||||
if ((timer == nil || !timer!.isValid) && atShortcut.state != .up) {
|
||||
let repeatRate = ticksToSeconds(defaults.string(forKey: "KeyRepeat") ?? "6")
|
||||
let initialDelay = ticksToSeconds(defaults.string(forKey: "InitialKeyRepeat") ?? "25")
|
||||
timer = Timer(fire: Date(timeIntervalSinceNow: initialDelay), interval: repeatRate, repeats: true, block: { _ in
|
||||
if atShortcut.state == .up {
|
||||
timer?.invalidate()
|
||||
} else {
|
||||
DispatchQueue.main.async {
|
||||
block()
|
||||
}
|
||||
}
|
||||
})
|
||||
timer = Timer(fire: Date(timeIntervalSinceNow: initialDelay), interval: repeatRate, repeats: true) { _ in
|
||||
handleEvent(atShortcut, block)
|
||||
}
|
||||
timer!.tolerance = repeatRate * 0.1
|
||||
CFRunLoopAddTimer(BackgroundWork.repeatingKeyThread.runLoop, timer!, .defaultMode)
|
||||
}
|
||||
}
|
||||
|
||||
private static func handleEvent(_ atShortcut: ATShortcut, _ block: @escaping () -> Void) {
|
||||
DispatchQueue.main.async {
|
||||
if atShortcut.state == .up {
|
||||
timer?.invalidate()
|
||||
} else {
|
||||
block()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static func ticksToSeconds(_ appleNumber: String) -> Double {
|
||||
return Double(appleNumber)! / 60 // Apple probably hard-coupled key repeat-rate with 60hz monitors
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user