How to programmatically make cocoa application active

ε祈祈猫儿з 提交于 2019-11-27 12:56:11

问题


I've got a background process that makes a transparent window appear when a hotkey is pressed:

[window makeKeyAndOrderFront:nil];
[[content animator] setAlphaValue:1.0]; // alpha was 0.0

... the window shows up fine & in front of the other windows (as I want it to), however until I manually click the window whatever application was active when the window appears remains active. I was expecting the 'makeKeyAndOrderFront' to make the application active as well, however adding a NSLog line to my -applicationWillBecomeActive shows it's not getting any active notification until the mouse click is performed.

Does anyone know how I can set my application active @ the same time I issue the -makeKeyAndOrderFront ? I need it active so that it can begin accepting keyboard input - any assistance needed :-)


回答1:


Look at [[NSApplication sharedApplication] activateIgnoringOtherApps : YES];.




回答2:


For 10.6 or greater

[[NSRunningApplication currentApplication] activateWithOptions:(NSApplicationActivateAllWindows | NSApplicationActivateIgnoringOtherApps)];



回答3:


Swift 4.2+ of @Arvin answer

NSRunningApplication.current.activate(options: [.activateIgnoringOtherApps, .activateAllWindows])


来源:https://stackoverflow.com/questions/1904690/how-to-programmatically-make-cocoa-application-active

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!