Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift and my Idle timer implementation ( missing CGEventType )

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.

like image 357
Antwan van Houdt Avatar asked Aug 11 '15 14:08

Antwan van Houdt


1 Answers

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)
like image 173
mrb Avatar answered Nov 05 '22 22:11

mrb