道歉,如果這是一個愚蠢/簡單的問題,但仍然習慣了Mac領域的一切。用運行循環重寫main.m,但仍然進入NSApplicationMain調用?
戴夫是一種足以回答我在這裏一個問題: Modify NSEvent to send a different key than the one that was pressed
這導致了下面的代碼,它的偉大工程:
#import <Cocoa/Cocoa.h>
CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
//0x0b is the virtual keycode for "b"
//0x09 is the virtual keycode for "v"
if (CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode) == 0x0B) {
CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, 0x09);
}
return event;
}
int main(int argc, char *argv[]) {
//return NSApplicationMain(argc, (const char **)argv);
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
CFRunLoopSourceRef runLoopSource;
CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, kCGEventMaskForAllEvents, myCGEventCallback, NULL);
if (!eventTap) {
NSLog(@"Couldn't create event tap!");
exit(1);
}
runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
CFRunLoopRun();
CFRelease(eventTap);
CFRelease(runLoopSource);
[pool release];
exit(0);
}
由於一些有用的錯誤信息(不能有兩個「主要」方法)我發現我必須將這些代碼放在main.m
文件中(對吧?)。這意味着我正在覆蓋默認方法:
int main(int argc, char *argv[]) {
return NSApplicationMain(argc, (const char **)argv);
}
這意味着我的其他基於Objective-C的代碼都沒有觸發。但是,如果我取消註釋該位(或者嘗試調用NSApplicationMain),那麼main.m
運行循環是不運行的。
我想這對於一個經驗豐富的Mac小夥子來說相當簡單,但是我很難把頭圍繞在它周圍。謝謝。