X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=TargetMouseBtn.m;fp=TargetMouseBtn.m;h=5137f2be5cf4001f92ff1494c683090ff2f5de34;hp=0000000000000000000000000000000000000000;hb=530009447c5bbd360ac5023979cffc6d32a28df3;hpb=5f07baa8cb03a2f6aa06338bfeb31dad3c3ca29f diff --git a/TargetMouseBtn.m b/TargetMouseBtn.m new file mode 100644 index 0000000..5137f2b --- /dev/null +++ b/TargetMouseBtn.m @@ -0,0 +1,48 @@ +// +// TargetMouseBtn.m +// Enjoy +// +// Created by Yifeng Huang on 7/27/12. +// Copyright (c) 2012 __MyCompanyName__. All rights reserved. +// + +#import "TargetMouseBtn.h" + +@implementation TargetMouseBtn + +@synthesize which; + +-(NSString*) stringify { + return [[NSString alloc] initWithFormat: @"mbtn~%d", which]; +} + ++(TargetMouseBtn*) unstringifyImpl: (NSArray*) comps { + NSParameterAssert([comps count] == 2); + TargetMouseBtn* target = [[TargetMouseBtn alloc] init]; + [target setWhich: [[comps objectAtIndex:1] integerValue]]; + return target; +} + +-(void) trigger { + NSPoint mouseLoc = [NSEvent mouseLocation]; + CGEventType eventType = (which == kCGMouseButtonLeft) ? kCGEventLeftMouseDown : kCGEventRightMouseDown; + CGEventRef click = CGEventCreateMouseEvent(NULL, + eventType, + CGPointMake(mouseLoc.x, mouseLoc.y), + which); + CGEventPost(kCGHIDEventTap, click); + CFRelease(click); +} + +-(void) untrigger { + NSPoint mouseLoc = [NSEvent mouseLocation]; + CGEventType eventType = (which == kCGMouseButtonLeft) ? kCGEventLeftMouseUp : kCGEventRightMouseUp; + CGEventRef click = CGEventCreateMouseEvent(NULL, + eventType, + CGPointMake(mouseLoc.x, mouseLoc.y), + which); + CGEventPost(kCGHIDEventTap, click); + CFRelease(click); +} + +@end