Keith Smiley | About | RSS | Mastodon

NSWindow Global Hotkey

For quite a while I was having trouble dealing with a global show/hide hotkey for windows in Objective-C. Global hotkeys are already hard enough. Although MASShortcut has solved that. Yes I know of ShortcutRecorder but it's very dated (MASShortcut even uses blocks!).

I found that once I had the shortcut working I was having a hard time dealing with opening and closing, showing and hiding the application. What seemed to happen was when the method was called and [[NSRunningApplication currentApplication] isActive] was evaluated in an if statement along with an else clause, if the application was hidden using [[NSApplication sharedApplication] hide:self]; it was reevaluated and it hit the else case. This also happened with an if statement checking if the window was already visible with [myWindow isVisible] even with return; statements inserted in appropriate places.

My solution was adding NSNumbers acting as booleans to keep track of the value allowing me to avoid else statements altogether and use else ifs instead.

- (void)showHideMainWindow {
    NSNumber *wasActive = @NO;
    if ([[NSRunningApplication currentApplication] isActive]) {
        wasActive = @YES;
        NSNumber *wasOpen = @NO;
        if ([self.window isVisible]) {
            wasOpen = @YES;
            [self.window close];
            [[NSApplication sharedApplication] hide:self];
        } else if (![wasOpen boolValue]) {
            [self.window makeKeyAndOrderFront:self];
    } else if (![wasActive boolValue]) {
        [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
        [self.window makeKeyAndOrderFront:self];