I want to make an app that will force the user to take a break for a specific time. I can't find a way to do that. Until now, i was about to use CGEvent to recive a notification when a key is pressed, but work only for mouse input.
let eventMask = CGEventType.keyDown.rawValue //| (1 << CGEventType.keyUp.rawValue)
guard let eventTap = CGEvent.tapCreate(tap: .cgSessionEventTap,
place: .headInsertEventTap,
options: .defaultTap,
eventsOfInterest: CGEventMask(eventMask),
callback: myCGEventCallback,
userInfo: nil) else {
print("failed to create event tap")
exit(1)
}
let runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0)
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, .commonModes)
CGEvent.tapEnable(tap: eventTap, enable: true)
CFRunLoopRun()
Does not matter what event mask is set, callback is trigger only from mouse action.
So my question is split in two:
- Why above code does not trigger on keydown
- Is any other way to achieve that task?
Thanks.