If you reference ApplicationServices or higher, try:
CFTimeInterval idleTime = CGEventSourceSecondsSinceLastEventType(kCGEventSourceStateCombinedSessionState, kCGAnyInputEventType);
The time since the last event for the event source.
The kCGAnyInputEventType event type will report the latest timestamp for any input event, keyboard, mouse, or tablet. Various events defined by the system and the application do not affect this type of event time.
Again, publishing a program or application from a login session should use kCGEventSourceStateCombinedSessionState.
The device user space that interprets the hardware state and generates events must use the kCGEventSourceStateHIDS SystemState.
CG_EXTERN CFTimeInterval CGEventSourceSecondsSinceLastEventType (source CGEventSourceStateID, event type CGEventType) AVAILABLE_MAC_OS_X_VERSION_10_4_AND_LATER;
source share