OneChillDude
OneChillDude

Reputation: 8006

Program a full screen uncloseable window on OS X

We have a game at work where if you can send the security guy an email from someone's unlocked computer you get a prize. This Halloween I am setting a trap.

I have a simple program called systems-engage that starts a key listener and opens my inbox programmatically. When someone starts using the keyboard I want my program to launch a full-screen visual assault of horror film images with extremely loud screaming.

I can handle everything else mentioned, I just need a dead simple way to open a full screen window that can only be closed by an escape sequence I define in code.

I'm going for lowest hanging fruit here (Objective-C, C++, Java, python ruby, JavaScript hell whatever gets the job done quick and dirty.

I read a primer on opening a full screen window in Objective-C but it can be closed really easily. The point of this prank is to shame my co-worker for invading my computer for at least 10 or 20 seconds and I can't do that if he can just hit Appl-Q.

Happy Halloween!

Upvotes: 3

Views: 426

Answers (1)

Itai Ferber
Itai Ferber

Reputation: 29918

To get something like this with a Cocoa app, you can place the following code in your app delegate's - (void)applicationDidFinishLaunching: (or similar):

// Set the key equivalent of the "Quit" menu item to something other than ⌘-Q.
// In this case, ^-⌥-⌘-Q.
// !!! Verify this and make sure you remember it or else you're screwed. !!!
NSMenu *mainMenu = [NSApplication sharedApplication].mainMenu;
NSMenu *appMenu = [[mainMenu itemAtIndex:0] submenu];
NSMenuItem *quitItem = [appMenu itemWithTitle:@"Quit <Your App Name Here>"];
quitItem.keyEquivalentModifierMask = NSEventModifierFlagControl | NSEventModifierFlagOption | NSEventModifierFlagCommand;
quitItem.keyEquivalent = @"q";

// Enable "kiosk mode" -- when fullscreen, hide the dock and menu bar, and prevent the user from switching away from the app or force-quitting.
[NSApplication sharedApplication].presentationOptions = NSApplicationPresentationHideDock
                                                      | NSApplicationPresentationHideMenuBar
                                                      | NSApplicationPresentationDisableProcessSwitching
                                                      | NSApplicationPresentationDisableForceQuit
                                                      | NSApplicationPresentationDisableSessionTermination;

// Remove the window's close button, making it no longer close with ⌘-W.
self.window.styleMask = self.window.styleMask & ~NSWindowStyleMaskClosable;

// Make the window take up the whole screen and make it full-screen.
[self.window setFrame:[[NSScreen mainScreen] frame] display:YES];
[self.window toggleFullScreen:self];

This will make a "kiosk" type app which can only be closed via the custom quit shortcut you set (or, you know, shutting down the computer forcibly). The presentation options prevents the user from accessing the menu bar, dock, and app switching (via ⌘-Tab) or spaces, bringing up the force-quit window, or bringing up the shutdown/restart/sleep window. Basically, make sure you set up a keyboard shortcut that you remember to terminate the app, otherwise, you're going to be locked out of your machine short of forcibly powering it off. It's a total PITA.

Of course, some of these customizations can be done in Interface Builder too (setting the key equivalent of the "Quit" menu item is easier there, and you can turn off the window's close control as well, as mentioned in comments above), but I just wanted to include this as code so it was more transparent (rather than uploading an Xcode project).

Happy Halloween! 😈

Upvotes: 3

Related Questions