In objective-c I used to implement my idle timer by basing it on the following CoreGraphics call:
CGEventSourceSecondsSinceLastEventType(kCGEventSourceStateCombinedSessionState, kCGAnyInputEventType);
Now in swift, using the same basic call, It'd look like:
CGEventSourceSecondsSinceLastEventType(CGEventSourceStateID.CombinedSessionState, CGEventType.MouseMoved)
After examining the header files, where for instance CGEventType is defined, I could not find a mention to any constant that does anything in the form of which I was able to do in the ObjC implementation.
Now I could probably workaround this by looking up the value of the constant and going about it like that, but I'd strongly prefer not to. I'd rather rewrite using IOKit instead then. It looks like it basically is defined as (~0), but like mentioned earlier, I rather not hardcode it like that.
Since the ~0
definition is a header #define (in CGEventTypes.h), it's unlikely to change because doing so would break binary compatibility. It should be safe to hardcode that value.
So the problem is getting a CGEventType that represents kCGAnyInputEventType
(aka ~0
).
Here's a reasonably type-safe way of representing that:
let anyInputEventType = CGEventType(rawValue: ~0)!
CGEventSourceSecondsSinceLastEventType(CGEventSourceStateID.CombinedSessionState, anyInputEventType)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With