Created
January 13, 2018 21:19
-
-
Save goloveychuk/20ded866d3db5743b56c35e46a1f1ce6 to your computer and use it in GitHub Desktop.
fix batterfly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import QuartzCore | |
let BLACK_LISTED: [Int64] = [8, 36] | |
var TIMEOUT = 100 | |
struct State { | |
var lastTimestamp: CGEventTimestamp? = nil | |
var lastEvType: CGEventType? = nil | |
} | |
var state = State() | |
func callback(proxy: CGEventTapProxy, evType: CGEventType, ev: CGEvent, ref: UnsafeMutableRawPointer?) -> Unmanaged<CGEvent>? { | |
let keycode = ev.getIntegerValueField(.keyboardEventKeycode) | |
let origEv = Unmanaged.passUnretained(ev) | |
if !BLACK_LISTED.contains(keycode) { | |
return origEv | |
} | |
guard let lastEvT = state.lastEvType else { | |
state.lastEvType = evType | |
return origEv | |
} | |
state.lastEvType = evType | |
guard lastEvT == .keyUp && evType == .keyDown else { | |
return origEv | |
} | |
guard let lastT = state.lastTimestamp else { | |
state.lastTimestamp = ev.timestamp | |
return origEv | |
} | |
state.lastTimestamp = ev.timestamp | |
let diff = (ev.timestamp - lastT) | |
if (diff < TIMEOUT * 1_000_000) { | |
print(diff, "blocked") | |
return nil | |
} | |
return origEv | |
} | |
let EVENTS = CGEventMask(1<<CGEventType.keyDown.rawValue | 1<<CGEventType.keyUp.rawValue) | |
let tap = CGEvent.tapCreate(tap: .cgSessionEventTap, place: .tailAppendEventTap, options: .defaultTap, eventsOfInterest: EVENTS, callback: callback, userInfo: nil | |
)! | |
let source = CFMachPortCreateRunLoopSource(nil, tap, 0)! | |
CFRunLoopAddSource(CFRunLoopGetCurrent()!, source, CFRunLoopMode.commonModes) | |
CFRunLoopRun() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment