Guest User

Untitled

a guest
May 27th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. import Foundation
  2. import AppKit
  3. import IOKit
  4. import IOKit.hid
  5.  
  6. let manager = IOHIDManagerCreate(
  7. kCFAllocatorDefault,
  8. IOOptionBits(kIOHIDOptionsTypeNone)
  9. )
  10.  
  11. var locked = false
  12.  
  13. @discardableResult
  14. func shell(_ args: String...) -> Int32 {
  15. let task = Process()
  16. task.launchPath = "/usr/bin/env"
  17. task.arguments = args
  18. task.launch()
  19. task.waitUntilExit()
  20. return task.terminationStatus
  21. }
  22.  
  23. func runScript() {
  24.  
  25. shell("bash", "/Users/max.chuquimia/xcode/KeyMonitor/run.sh")
  26. print("unlocking soon")
  27.  
  28. // Don't allow multiple presses quickly
  29. DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
  30. locked = false
  31. print("unlocked")
  32. }
  33. }
  34.  
  35. let matchDict: [CFString: NSNumber] = [
  36. kIOHIDVendorIDKey as CFString: NSNumber(value: 5050),
  37. kIOHIDProductIDKey as CFString: NSNumber(value: 1),
  38. ]
  39.  
  40. IOHIDManagerSetDeviceMatching(manager, matchDict as CFDictionary)
  41.  
  42. // IOHIDManagerRegisterDeviceMatchingCallback(manager, { (_, _, _, inIOHIDDeviceRef) in
  43. // print(inIOHIDDeviceRef)
  44. // }, nil)
  45.  
  46. IOHIDManagerScheduleWithRunLoop( manager, CFRunLoopGetMain(), CFRunLoopMode.defaultMode!.rawValue);
  47.  
  48. IOHIDManagerRegisterInputValueCallback(manager, { (context, inResult, inSent, value) in
  49. guard !locked else { return }
  50. locked = true
  51. print("INPUT DETECTED")
  52. print(IOHIDValueGetIntegerValue(value))
  53. runScript()
  54. }, nil)
  55.  
  56.  
  57. let result = IOHIDManagerOpen(manager, IOOptionBits(kIOHIDOptionsTypeNone))
  58.  
  59. assert(result == kIOReturnSuccess)
  60.  
  61. CFRunLoopRun()
Add Comment
Please, Sign In to add comment