Notification after a user is idle in OS X?

What is the best way to detect when the user has been idle for X times and then detects when the user immediately becomes active?

I know that NSWorkspace, which provides notifications about sleep / wakefulness / done, but I can’t rely on this because setting up sleep usually takes about 15 minutes to never. I need to find out if the user is idle for 1-2 minutes.

This answer provides an opportunity to get downtime. I would like to avoid polling if possible.

+4
source share
1 answer
As far as I know, a survey is your only option. As user1118321 points out, polling each O (minutes) is unlikely to cause any problems, performance, or otherwise.

If your application has a graphical interface and still accepts user interface events, you can set the handler via +[NSEvent addLocalMonitorForEventsMatchingMask:handler:] , which resets your timer on each event. This will help reduce, if not eliminate, the polls when the user is constantly active, at least in their application.

Once you have determined that the user has been idle for a long time, you can set a global response to the event to view the next event. See For example -[NSEvent addGlobalMonitorForEventsMatchingMask:handler:] .

Note: you should use CGEventSourceSecondsSinceLastEventType , if at all possible, and not squeeze into the IO registry. This is a formal, supported API and may be more efficient. Not to mention this easier. There is also UKIdleTimer , although it relies on Carbon, so it may not apply.

+2
source

Source: https://habr.com/ru/post/1446579/


All Articles