e8d77e42f6c67364557d835ef860a0e00d4383bd
[enjoyable.git] / TargetMouseBtn.m
1 //
2 // TargetMouseBtn.m
3 // Enjoy
4 //
5 // Created by Yifeng Huang on 7/27/12.
6 // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "TargetMouseBtn.h"
10
11 @implementation TargetMouseBtn
12
13 @synthesize which;
14
15 -(NSString*) stringify {
16 return [[NSString alloc] initWithFormat: @"mbtn~%u", which];
17 }
18
19 +(TargetMouseBtn*) unstringifyImpl: (NSArray*) comps {
20 NSParameterAssert([comps count] == 2);
21 TargetMouseBtn* target = [[TargetMouseBtn alloc] init];
22 [target setWhich: [comps[1] integerValue]];
23 return target;
24 }
25
26 -(void) trigger {
27 NSRect screenRect = [[NSScreen mainScreen] frame];
28 NSInteger height = screenRect.size.height;
29 NSPoint mouseLoc = [NSEvent mouseLocation];
30 CGEventType eventType = (which == kCGMouseButtonLeft) ? kCGEventLeftMouseDown : kCGEventRightMouseDown;
31 CGEventRef click = CGEventCreateMouseEvent(NULL,
32 eventType,
33 CGPointMake(mouseLoc.x, height - mouseLoc.y),
34 which);
35 CGEventPost(kCGHIDEventTap, click);
36 CFRelease(click);
37 }
38
39 -(void) untrigger {
40 NSRect screenRect = [[NSScreen mainScreen] frame];
41 NSInteger height = screenRect.size.height;
42 NSPoint mouseLoc = [NSEvent mouseLocation];
43 CGEventType eventType = (which == kCGMouseButtonLeft) ? kCGEventLeftMouseUp : kCGEventRightMouseUp;
44 CGEventRef click = CGEventCreateMouseEvent(NULL,
45 eventType,
46 CGPointMake(mouseLoc.x, height - mouseLoc.y),
47 which);
48 CGEventPost(kCGHIDEventTap, click);
49 CFRelease(click);
50 }
51
52 @end