使用 DDHotKey 包装器进行 cocoa/carbon 实例化 NSWindow

发布于 2024-10-18 05:19:11 字数 519 浏览 6 评论 0原文

在 Dave DeLong 和 stackoverflow 上其他人的帮助下,我为我的教程应用程序提供了一个很酷的热键效果,但我不知道如何让它实例化窗口。

我有以下设置:

首次运行时不处于活动状态的计算器,但具有 NSStatusItem 图标和菜单,以及可打开主窗口的菜单选项。

另外,我已将 DDHotKeyCenter.h 和 DDHotKeyCenter 添加到目录并链接到 Carbon.framework。

NSStatusMenu 通过以下方式连接到窗口:

-(IBAction)activateMain:(id)sender{
  [NSApp activateIgnoringOtherApps:YES];}

我想知道是否可以使用 Blocks 方法将热键触发的操作直接连接到 IBAction,或者是否有一些中间步骤来连接它们?

让 DDHotKey 触发 NSEvent 会更好吗?或者它甚至可以做到这一点吗?

我似乎对其实现有点困惑。

With the help of Dave DeLong and others on stackoverflow I've given my tutorial app a cool hotkey effect but I can't figure out how to make it instantiate the window.

I have the following setup:

A calculator that is not active when first run but has a NSStatusItem icon and menu, with a menu option that opens the main window.

Also, I have added DDHotKeyCenter.h and DDHotKeyCenter to the directory + linked the Carbon.framework.

The NSStatusMenu is connected to the window through:

-(IBAction)activateMain:(id)sender{
  [NSApp activateIgnoringOtherApps:YES];}

What I was wondering is if it's possible to connect the actions fired by the hotkey, using the Blocks method, to the IBAction directly, or if there's some intermediate step to connect them?

Would it be better to have the DDHotKey fire an NSEvent, or can it even do that?

I seem to be a bit confused about it's implementation.

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

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

发布评论

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

评论(1

肤浅与狂妄 2024-10-25 05:19:11

DDHotKey 不会“触发 NSEvent”。它调用对象上的方法。您可以非常轻松地设置热键来触发拥有它的任何对象的 activateMain: 方法:

...
DDHotKeyCenter * c = [[DDHotKeyCenter alloc] init];
[c registerHotKeyWithKeyCode... target:self action:@selector(activateMain:) object:nil];
...

或者如果您想使用块,您可以这样做:

...
DDHotKeyTask task = ^(NSEvent *hkEvent) {
    [NSApp activateIgnoringOtherApps:YES];
};
DDHotKeyCenter * c = [[DDHotKeyCenter alloc] init];
[c registerHotKeyWithKeyCode... task:task];
...

DDHotKey does not "fire an NSEvent". It invokes a method on an object. You could very easily set up your hotkey to fire the activateMain: method of whatever object owns it:

...
DDHotKeyCenter * c = [[DDHotKeyCenter alloc] init];
[c registerHotKeyWithKeyCode... target:self action:@selector(activateMain:) object:nil];
...

Or if you wanted to use a block, you could do:

...
DDHotKeyTask task = ^(NSEvent *hkEvent) {
    [NSApp activateIgnoringOtherApps:YES];
};
DDHotKeyCenter * c = [[DDHotKeyCenter alloc] init];
[c registerHotKeyWithKeyCode... task:task];
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文