1. Using AppleScript in Cocoa
NSAppleScript *script = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"yourFile" ofType:@"scpt"]] error: nil];
[script executeAndReturnError: NULL];
2. Using EventTap
First, we should create an
EventTap
, which captures system defined gestures include
NSEventMaskGesture
, NSEventMaskMagnify
, NSEventMaskSwipe
, NSEventMaskRotate
, NSEventMaskBeginGesture
, and NSEventMaskEndGesture
.
You can capture whatever gestures defined before by add a mask using
CGEventMask.
The following just captures swipe gestures:
In AppDelegate.m:
- (void)createEventTap
{
CFRunLoopSourceRef runLoopSource;
CGEventMask eventMask = (NSEventMaskSwipe);
eventTap = CGEventTapCreate(kCGHIDEventTap,
kCGHeadInsertEventTap,
kCGEventTapOptionDefault,
eventMask,
MyCGEventCallback,
NULL);
runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault,
eventTap,
0);
CFRunLoopAddSource(CFRunLoopGetCurrent(),
runLoopSource,
kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
}
In method applicationDidFinishLaunching:
[self createEventTap];
Then you can use the gesture captured in your
CGEventCallback
, like:
CGEventRef MyCGEventCallback(CGEventTapProxy proxy,
CGEventType type,
CGEventRef theEvent,
void *refcon)
{
return NULL;
}
In
MyCGEventCallback
, if you return NULL, then the system just ignores the gesture, which means our work is partly done. But we may want to use the gesture instead of just disable it. Then we can use touchEvents defined in Cocos2d, which defined as:
-(BOOL) ccTouchesBeganWithEvent:(NSEvent *)event;
-(BOOL) ccTouchesMovedWithEvent:(NSEvent *)event;
-(BOOL) ccTouchesEndedWithEvent:(NSEvent *)event;
-(BOOL) ccTouchesCancelledWithEvent:(NSEvent *)event;
In fact, they are defined in origin as:
- (void)touchesBeganWithEvent:(NSEvent *)event;
- (void)touchesMovedWithEvent:(NSEvent *)event;
- (void)touchesEndedWithEvent:(NSEvent *)event;
- (void)touchesCancelledWithEvent:(NSEvent *)event;
If you are using a CCLayer, use the first one. Otherwise, use the second one.
A simple example:
-(BOOL) ccTouchesBeganWithEvent:(NSEvent *)event
{
//NSLog(@"touch begin");
NSSet * touches = [event touchesMatchingPhase:NSTouchPhaseBegan inView:[[CCDirectorMac sharedDirector] openGLView]];
[player onTouchBegan:[touches retain]];
return YES;
}