-
Notifications
You must be signed in to change notification settings - Fork 56
/
TargetMouseBtn.m
52 lines (44 loc) · 1.76 KB
/
TargetMouseBtn.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// 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~%u", which];
}
+(TargetMouseBtn*) unstringifyImpl: (NSArray*) comps {
NSParameterAssert([comps count] == 2);
TargetMouseBtn* target = [[TargetMouseBtn alloc] init];
[target setWhich: [[comps objectAtIndex:1] integerValue]];
return target;
}
-(void) trigger: (JoystickController *)jc {
NSRect screenRect = [[NSScreen mainScreen] frame];
NSInteger height = screenRect.size.height;
NSPoint mouseLoc = [NSEvent mouseLocation];
CGEventType eventType = (which == kCGMouseButtonLeft) ? kCGEventLeftMouseDown : kCGEventRightMouseDown;
CGEventRef click = CGEventCreateMouseEvent(NULL,
eventType,
CGPointMake(mouseLoc.x, height - mouseLoc.y),
which);
CGEventPost(kCGHIDEventTap, click);
CFRelease(click);
}
-(void) untrigger: (JoystickController *)jc {
NSRect screenRect = [[NSScreen mainScreen] frame];
NSInteger height = screenRect.size.height;
NSPoint mouseLoc = [NSEvent mouseLocation];
CGEventType eventType = (which == kCGMouseButtonLeft) ? kCGEventLeftMouseUp : kCGEventRightMouseUp;
CGEventRef click = CGEventCreateMouseEvent(NULL,
eventType,
CGPointMake(mouseLoc.x, height - mouseLoc.y),
which);
CGEventPost(kCGHIDEventTap, click);
CFRelease(click);
}
@end