Search code examples
macosdelphidelphi-10-seattle

Detect Idle Time in Multi-Device Application on OSX


I need to detect Idle Time, Time Since the user has input anything into their machine, I've made this application before for windows only and this function worked brilliantly :-

function IdleTime: DWord;
var
  LastInput: TLastInputInfo;
begin
  LastInput.cbSize := SizeOf(TLastInputInfo);
  GetLastInputInfo(LastInput);
  Result := (GetTickCount - LastInput.dwTime) DIV 1000;
end;

However, this function doesn't work on Multi-Device Application(as far as I can tell). I've messed around with this for a while now and done some severe googling to no avail.

The target OS is OS X and Windows.


Solution

  • The equivalent of GetLastInputInfo on OSX is CGEventSourceCounterForEventType.

    See: https://developer.apple.com/library/mac/documentation/Carbon/Reference/QuartzEventServicesRef/index.html#//apple_ref/c/func/CGEventSourceCounterForEventType

    See here: Detecting user-activity on mac os x

    The API interface for this call is in: Macapi.CoreGraphics
    So you'll need to add that unit to your uses clause.

    If you're not familair with OSX programming under Delphi, have a look at:https://delphihaven.wordpress.com