在OS X上编写一个全屏不可关闭的窗口

Program a full screen uncloseable window on OS X

本文关键字:窗口 一个 OS      更新时间:2023-10-16

我们公司有一个游戏,如果你能从别人未解锁的电脑上给安全人员发一封电子邮件,你就能得到奖品。这个万圣节我要设一个陷阱。

我有一个叫做systems-engage的简单程序,它启动一个键侦听器并以编程方式打开我的收件箱。当有人开始使用键盘时,我希望我的程序启动一个恐怖电影图像的全屏视觉攻击,伴随着极其响亮的尖叫。

我只需要一个非常简单的方法来打开一个全屏窗口,只能通过我在代码中定义的转义序列来关闭。

我要用最容易摘到的果子(Objective-C, c++, Java, python, ruby, JavaScript等等,只要能快速完成任务就行。

我读了一篇关于在Objective-C中打开全屏窗口的入门文章,但它可以很容易地关闭。这个恶作剧的目的是让我的同事感到羞耻,因为他侵入了我的电脑至少10到20秒,如果他能按下apple - q键,我就做不到。

万圣节快乐!

要在Cocoa应用程序中获得类似的东西,您可以在应用程序委托的- (void)applicationDidFinishLaunching:(或类似)中放置以下代码:

// 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];

这将创建一个"kiosk";输入只能通过你设置的自定义退出快捷方式关闭的应用程序(或者,你知道,强制关闭计算机)。演示选项阻止用户访问菜单栏、dock和应用程序切换(通过⌘-Tab)或空格,调出强制退出窗口或调出关机/重新启动/睡眠窗口。基本上,确保你设置了一个键盘快捷键,你记得终止应用程序,否则,你将被锁定在你的机器,除非强行关闭它。这是一个完整的PITA。

当然,其中一些自定义也可以在Interface Builder中完成(设置键相当于"退出")。菜单项在那里更容易,你也可以关闭窗口的关闭控制,正如上面的评论所提到的),但我只是想把它作为代码包含,这样它就更透明了(而不是上传一个Xcode项目)。

万圣节快乐!