Forked from cprovatas/gist:6acef442fc43123bcd5d5e937dc7951a
Created
July 16, 2018 05:14
-
-
Save goliatone/19271d6bba18ea49f4c3b7ba83a000df to your computer and use it in GitHub Desktop.
Monitor Mouse Event for any arbitrary application on macOS in Swift
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
let callback: CGEventTapCallBack = { (tapProxy, eventType, event, refcon) -> Unmanaged<CGEvent>? in | |
debugPrint("we are monitoring the mouse event here") | |
return nil | |
} | |
let eventMask = (1 << CGEventType.leftMouseDown.rawValue) | (1 << CGEventType.leftMouseUp.rawValue) | |
let machPort = CGEvent.tapCreateForPid(pid: 40529, place: .tailAppendEventTap, options: .defaultTap, eventsOfInterest: CGEventMask(eventMask), callback: callback, userInfo: nil)! | |
let runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, machPort, 0) | |
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, .commonModes) | |
CGEvent.tapEnable(tap: machPort, enable: true) | |
CFRunLoopRun() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment