0

My application processes keyboard buttons touches. It acts as a system-wide key touch listener and uses CGEvent for this purpose.

Everything goes fine and the app is immediately responsive to any key touch, but if the application gets idle for several seconds (about 6-7 seconds) then I notice that it gets lowered in its processing priority: it fails to respond immediately to the next key touch, there is a noticeable delay in response to the first key touch after this idle pause. This key touch "awakens" the application and it becomes 100% responsive again. But next idle pause in application activity for several seconds - and it again gets in a lower-responsive "sleep" state.

I try to keep my application always "awake" by performing some forced regular activity which prevents the app from going to a lower-responsive "sleep" state, but I want to know if there is any "legal" way to keep application always 100% responsive and prevent it from going to this "sleep" which is obviously some macOS feature to optimize system load.

EDIT: About the App Nap. The code like this:

self.myActivity = [[NSProcessInfo processInfo] beginActivityWithOptions:NSActivityUserInitiatedAllowingIdleSystemSleep reason: @"Good reason"];

does not affect the described behaviour of the application. Moreover, System Monitor tool indicates that the application is not in App Nap mode. Moreover, if to disable App Nap system-wide like this:

defaults write NSGlobalDomain NSAppSleepDisabled -bool YES

then this does not affect the described behaviour of the application. So, this is not App Nap.

EDIT: This is how keyboard events are detected:

CFMachPortRef keyEventTap = CGEventTapCreate(kCGSessionEventTap,
                                             kCGHeadInsertEventTap,
                                             kCGEventTapOptionListenOnly,
                                             CGEventMaskBit(kCGEventKeyDown) | CGEventMaskBit(kCGEventKeyUp) | CGEventMaskBit(kCGEventFlagsChanged),
                                             keyEventCallback,
                                             (__bridge void *)self);
if (keyEventTap) {

    self.runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, keyEventTap, 0);
    CFRunLoopRef runLoopCurrent = CFRunLoopGetCurrent();
    CFRunLoopAddSource(runLoopCurrent, self.runLoopSource, kCFRunLoopCommonModes);
}

CGEvent events do not become disabled at runtime, so checking for kCGEventTapDisabledByTimeout event type has no effect.

Kibernetik
  • 2,947
  • 28
  • 35
  • Cant this functionality be done as a kernel extension? – Richard Barber Feb 12 '20 at 07:17
  • Does this answer your question? [Disable app nap in Swift](https://stackoverflow.com/questions/27653939/disable-app-nap-in-swift) – Willeke Feb 12 '20 at 10:14
  • Possible duplicate of [Disable App Nap in MacOS 10.9 (Mavericks) application](https://stackoverflow.com/questions/19847293/disable-app-nap-in-macos-10-9-mavericks-application) – Willeke Feb 12 '20 at 10:16
  • Are you using `CGEventTap`? Are you checking for `kCGEventTapDisabledByTimeout`? – TheNextman Feb 12 '20 at 16:46

0 Answers0