Last active
May 23, 2018 06:42
-
-
Save maxchuquimia/259fb4baed6e1847a505ae036e3a9c6d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Foundation | |
| import AppKit | |
| import IOKit | |
| import IOKit.hid | |
| let manager = IOHIDManagerCreate( | |
| kCFAllocatorDefault, | |
| IOOptionBits(kIOHIDOptionsTypeNone) | |
| ) | |
| var locked = false | |
| @discardableResult | |
| func shell(_ args: String...) -> Int32 { | |
| let task = Process() | |
| task.launchPath = "/usr/bin/env" | |
| task.arguments = args | |
| task.launch() | |
| task.waitUntilExit() | |
| return task.terminationStatus | |
| } | |
| func runScript() { | |
| shell("bash", "/Users/max.chuquimia/xcode/KeyMonitor/run.sh") | |
| print("unlocking soon") | |
| // Don't allow multiple presses quickly | |
| DispatchQueue.main.asyncAfter(deadline: .now() + 3) { | |
| locked = false | |
| print("unlocked") | |
| } | |
| } | |
| let matchDict: [CFString: NSNumber] = [ | |
| kIOHIDVendorIDKey as CFString: NSNumber(value: 5050), | |
| kIOHIDProductIDKey as CFString: NSNumber(value: 1), | |
| ] | |
| IOHIDManagerSetDeviceMatching(manager, matchDict as CFDictionary) | |
| // IOHIDManagerRegisterDeviceMatchingCallback(manager, { (_, _, _, inIOHIDDeviceRef) in | |
| // print(inIOHIDDeviceRef) | |
| // }, nil) | |
| IOHIDManagerScheduleWithRunLoop( manager, CFRunLoopGetMain(), CFRunLoopMode.defaultMode!.rawValue); | |
| IOHIDManagerRegisterInputValueCallback(manager, { (context, inResult, inSent, value) in | |
| guard !locked else { return } | |
| locked = true | |
| print("INPUT DETECTED") | |
| print(IOHIDValueGetIntegerValue(value)) | |
| runScript() | |
| }, nil) | |
| let result = IOHIDManagerOpen(manager, IOOptionBits(kIOHIDOptionsTypeNone)) | |
| assert(result == kIOReturnSuccess) | |
| CFRunLoopRun() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment