-3

I am looking to develop an application in swift that will post the attendance and if no key is pressed by keyboard or mouse(means the person is not working) the application mark the break. The main issue I'm facing is I'm unable to get the idle time.

  • Possible duplicate of [Notification when the system is idle or not on OS X](https://stackoverflow.com/questions/6418210/notification-when-the-system-is-idle-or-not-on-os-x) – Willeke Jul 28 '18 at 13:03
  • Possible duplicate of [Best way to know if application is inactive in cocoa mac OSX?](https://stackoverflow.com/questions/3547601/convert-objective-c-code-to-c-for-detecting-user-idle-time-on-os-x) – Willeke Jul 28 '18 at 13:04

1 Answers1

1

X-Y question :)

You don't need to capture keyboard or mouse events to get idle time on macOS -- it's built right in. I googled for "macos idle time" and came across this gist:

#!/bin/sh

# Get MacOSX idletime. Shamelessly stolen from https://macscripter.net/viewtopic.php?pid=144392
/usr/sbin/ioreg -c IOHIDSystem | /usr/bin/awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'

Now you can either use that, or find out the Swift API to read the same value.

AKX
  • 152,115
  • 15
  • 115
  • 172