簡體   English   中英

從C ++方法向Objective-C方法傳遞消息?

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

我正在閱讀有關創建全局熱鍵的文章。 我已經成功地完成了本教程,但是現在我試圖向Objective-C方法發送消息,但我陷入了困境。 有沒有辦法從C ++代碼向Objective-C傳遞消息?

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

這是我的代碼所在的位置:

#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對象。源文件。

甚至不需要更改文件擴展名。 只需在C ++文件上執行RMB /“ show info”或任何操作,然后將類型從“ cpp”更改為“ objective_c / cpp”或其他操作即可(我忘記了實際值是多少)。

然后,您可以將C ++和Objective-C混合到您的心臟中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM