简体   繁体   English

从C ++方法向Objective-C方法传递消息?

[英]Messaging Objective-C method from C++ method?

I'm reading this article about creating a global hotkey. 我正在阅读有关创建全局热键的文章。 I've gone through the tutorial successfully, but now I'm trying to message an Objective-C method, and I'm stuck. 我已经成功地完成了本教程,但是现在我试图向Objective-C方法发送消息,但我陷入了困境。 Is there a way to message Objective-C from C++ code? 有没有办法从C ++代码向Objective-C传递消息?

http://cocoasamurai.blogspot.com/2009/03/global-keyboard-shortcuts-with-carbon.html http://cocoasamurai.blogspot.com/2009/03/global-keyboard-shortcuts-with-carbon.html

Here's where my code is at: 这是我的代码所在的位置:

#import "AppDelegate.h"
#import <Carbon/Carbon.h>

@implementation AppDelegate

@synthesize window = _window;
@synthesize statusItem;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    EventHotKeyRef myHotKeyRef;
    EventHotKeyID myHotKeyID;
    EventTypeSpec keyPressedEventType;
    EventTypeSpec keyReleaseEventType;

    keyPressedEventType.eventClass=kEventClassKeyboard;
    keyPressedEventType.eventKind=kEventHotKeyPressed;

    keyReleaseEventType.eventClass=kEventClassKeyboard;
    keyReleaseEventType.eventKind=kEventHotKeyReleased;

    InstallApplicationEventHandler(&keyPressedHandler, 1, &keyPressedEventType, NULL, NULL);
    InstallApplicationEventHandler(&keyReleasedHandler, 1, &keyReleaseEventType, NULL, NULL);

    myHotKeyID.signature='mhk1';
    myHotKeyID.id=1;

    RegisterEventHotKey(97, 0, myHotKeyID, GetApplicationEventTarget(), 0, &myHotKeyRef);
}

- (void)awakeFromNib
{
    statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
    [statusItem setMenu:statusMenu];
    [statusItem setImage:[NSImage imageNamed:@"microphone_muted"]];
    [statusItem setAlternateImage:[NSImage imageNamed:@"neg_microphone_muted"]];
    [statusItem setHighlightMode:YES];
}
- (void) mute
{
    [statusItem setImage:[NSImage imageNamed:@"microphone_muted"]];
    [statusItem setAlternateImage:[NSImage imageNamed:@"neg_microphone_muted"]];
}
- (void) unmute 
{
    [statusItem setImage:[NSImage imageNamed:@"microphone"]];
    [statusItem setAlternateImage:[NSImage imageNamed:@"neg_microphone"]];
}
OSStatus keyPressedHandler(EventHandlerCallRef nextHandler, EventRef anEvent, void *userData)
{   
    NSLog(@"Unmute mic");
    return noErr; 
}
OSStatus keyReleasedHandler(EventHandlerCallRef nextHandler, EventRef anEvent, void *userData)
{
    NSLog(@"Mute mic");
    return noErr; 
}
@end

如果您的C ++源文件具有.mm扩展名(而不是.cpp ),则它将被编译为Objective-C ++,您将能够像使用标准.m一样将消息发送到Objective-C对象。源文件。

No need to even change the file extension. 甚至不需要更改文件扩展名。 Just do RMB/"show info" or whatever on your C++ file and change the type from "cpp" to "objective_c/cpp" or whatever (I forget what the actual values are). 只需在C ++文件上执行RMB /“ show info”或任何操作,然后将类型从“ cpp”更改为“ objective_c / cpp”或其他操作即可(我忘记了实际值是多少)。

Then you can intermix C++ and Objective-C to your heart's content. 然后,您可以将C ++和Objective-C混合到您的心脏中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM