What methods can determine the user leaves the computer, and then lock the computer. For example windows' C++ method GetLastInputInfo()
You can use CGEventSourceSecondsSinceLastEventType() or CGEventSourceCounterForEventType() to determine if the user is idle. Note, though, that the user may not give any input for long periods even though they're at the computer. For example, they may be watching a long video. A video-playing app would typically use one of the IOPMAssertionCreate…() functions to prevent the display from sleeping despite the lack of user input, so you might want to check for that using IOPMCopyAssertionsStatus(). You would want to avoid locking the computer if something has asserted that the display won't sleep.