我有一个用Objective-C编写的演示应用程序,它利用了Dave DeLong的DDHotKey类(这是一段很棒的代码,顺便说一下),我想知道在应用程序开始时,我应该在哪里让这个类启动?
具体地说,该类中有两个函数,registerhotkey (由Dave DeLong提供的示例代码中的registerexample1)和unregisterhotkey (由Dave DeLong提供的示例代码中的unregisterexample1),我希望它们分别在程序执行和关闭时运行。
我真的不确定如何做到这一点,我正在寻找一个指南,我应该在哪里看,或只是一些基本的指针。
谢谢!
发布于 2011-03-30 06:20:39
最简单的方法是在应用程序委托中的applicationDidFinishLaunching:方法中。这是在启动时调用的。当应用程序即将退出时,将调用applicationWillTerminate:方法。
// in application delegate
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
// call registerhotkey
}
- (void)applicationWillTerminate:(NSNotification *)notification {
// call unregisterhotkey
}或者,您可以将调用放在主函数中,在调用NSApplicationMain之前调用registerhotkey,在调用NSApplicationMain之后调用unregisterhotkey。如果还没有,你需要在这段代码周围添加一个自动释放池。
int main(int argc, char **argv) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// call registerhotkey
int result = NSApplicationMain(argc,argv);
// call unregisterhotkey
return result;
}最后,您可以使用特殊的load方法在装入类或类别时调用registerhotkey。您实际上不需要调用unregisterhotkey,因为当您的应用程序退出时,系统会自动调用它。
// in any class or category
+ (void)load {
// call registerhotkey
}https://stackoverflow.com/questions/5478482
复制相似问题