在 Objective-C 中应用程序启动时执行一个类

发布于 2024-10-28 00:45:44 字数 330 浏览 2 评论 0原文

我有一个用 Objective-C 编写的演示应用程序,它使用 Dave DeLong 的 DDHotKey 类(顺便说一句,这是一段出色的编码),我想知道我应该在哪里查看如何在应用程序启动时立即启动该类?

具体来说,该类中有两个函数,registerhotkey(Dave DeLong 提供的示例代码中的 registerexample1)和 unregisterhotkey(Dave DeLong 提供的示例代码中的 unregisterexample1),我想分别在程序执行和程序关闭时运行。

我不太确定如何做到这一点,并且正在寻找关于我应该在哪里查看的指南或只是一些基本的指导。

谢谢!

I have a demo application written in Objective-C that makes use of Dave DeLong's DDHotKey class (a brilliant piece of coding, btw) and I was wondering where should I look at getting the class to launch as soon as the application begins?

Specifically, there are two functions in the class, registerhotkey (registerexample1 in the example code provided by Dave DeLong) and unregisterhotkey (unregisterexample1 in the example code provided by Dave DeLong) that I would like to run at program execution and at program close respectively.

I'm not really sure how to do this and am looking either for a guide as to where I should look or just some basic pointers.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日久见人心 2024-11-04 00:45:44

最简单的方法是使用应用委托中的 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
}

The easiest way to do it is in the applicationDidFinishLaunching: method in your app delegate. This is called on startup. The applicationWillTerminate: method will be called when the application is about to exit.

// in application delegate
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
    // call registerhotkey
}
- (void)applicationWillTerminate:(NSNotification *)notification {
    // call unregisterhotkey
}

Alternatively, you could place the calls in your main function, calling registerhotkey before the call to NSApplicationMain, and unregisterhotkey after the call to NSApplicationMain. If there isn't one already, you will need to add an autorelease pool around this code.

int main(int argc, char **argv) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    // call registerhotkey
    int result = NSApplicationMain(argc,argv);
    // call unregisterhotkey
    return result;
}

Finally, you could use the special load method to call registerhotkey when a class or category is loaded. You actually don't need to call unregisterhotkey because the system will do it automatically when your application quits.

// in any class or category
+ (void)load {
    // call registerhotkey
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文