-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMacHackAppDelegate.m
183 lines (141 loc) · 4.89 KB
/
MacHackAppDelegate.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// MacHackAppDelegate.m
// MacHack
//
// Created by Jedediah Smith on 11-01-24.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "MacHackAppDelegate.h"
@implementation MacHackAppDelegate
@synthesize window;
- (void) toggleKeyFilter {
if ([keyFilter isEnabled])
[self disableKeyFilter];
else
[self enableKeyFilter];
}
- (void)enableKeyFilter {
[keyFilter enable];
[statusItem setTitle:@"H"];
[itemEnable setTitle:@"Disable"];
[self printToConsole:@"Enable key filter\n"];
}
- (void)disableKeyFilter {
[keyFilter disable];
[statusItem setTitle:@"h"];
[itemEnable setTitle:@"Enable"];
[self printToConsole:@"Disable key filter\n"];
}
- (void) showConsole {
if (console == nil) {
consoleWindow = [NSWindow alloc];
[consoleWindow initWithContentRect:NSMakeRect(100,100,300,200)
styleMask:NSTitledWindowMask |
NSClosableWindowMask |
NSMiniaturizableWindowMask |
NSResizableWindowMask
backing:NSBackingStoreBuffered
defer:false];
[consoleWindow setTitle:@"MacHack Console"];
[consoleWindow setReleasedWhenClosed:YES];
[consoleWindow setAutodisplay:true];
[consoleWindow setDelegate:self];
NSRect frame = [[consoleWindow contentView] frame];
NSScrollView* scrollView = [[NSScrollView alloc] initWithFrame:frame];
[scrollView setBorderType:NSNoBorder];
[scrollView setHasHorizontalScroller:true];
[scrollView setHasVerticalScroller:true];
[scrollView setAutohidesScrollers:true];
[scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[scrollView setAutoresizesSubviews:true];
console = [[NSTextView alloc] initWithFrame:frame];
[console setEditable:false];
[console setHorizontallyResizable:true];
[console setVerticallyResizable:true];
[console setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[console setFont:[NSFont userFixedPitchFontOfSize:12.0]];
NSTextContainer* textContainer = [console textContainer];
[textContainer setWidthTracksTextView:NO];
[textContainer setHeightTracksTextView:NO];
[textContainer setContainerSize:NSMakeSize(FLT_MAX,FLT_MAX)];
[scrollView setDocumentView:console];
[consoleWindow setContentView:scrollView];
[consoleWindow makeFirstResponder:console];
[consoleWindow orderFront:self];
[scrollView release];
[console release];
[keyFilter setConsole:self];
}
}
- (void) windowWillClose:(NSNotification *)notification {
if ([notification object] == consoleWindow)
console = nil;
}
- (void) hideConsole {
if (console != nil) {
[keyFilter setConsole:nil];
[consoleWindow close];
}
}
- (void) printToConsole:(NSString*) format, ... {
if (console != nil) {
va_list args;
va_start(args,format);
NSString* s = [[NSString alloc] initWithFormat:format arguments:args];
[console setString:[[console string] stringByAppendingString:s]];
[console scrollToEndOfDocument:self];
[s release];
va_end(args);
}
}
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
if (menuItem == itemConsole)
return console == nil;
return true;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
itemEnable = [NSMenuItem allocWithZone:[NSMenu menuZone]];
[itemEnable initWithTitle:@"Enable"
action:@selector(toggleKeyFilter)
keyEquivalent:@""];
[itemEnable setTarget:self];
itemConsole = [NSMenuItem allocWithZone:[NSMenu menuZone]];
[itemConsole initWithTitle:@"Console"
action:@selector(showConsole)
keyEquivalent:@""];
[itemConsole setTarget:self];
itemQuit = [NSMenuItem allocWithZone:[NSMenu menuZone]];
[itemQuit initWithTitle:@"Quit"
action:@selector(terminate:)
keyEquivalent:@""];
[itemQuit setTarget:NSApp];
NSMenu* menu = [NSMenu allocWithZone:[NSMenu menuZone]];
[menu initWithTitle:@"Menu"];
[menu addItem: itemEnable];
[menu addItem: itemConsole];
[menu addItem: itemQuit];
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
[statusItem retain];
[statusItem setTitle: @"h"];
[statusItem setHighlightMode:true];
[statusItem setMenu:menu];
[menu release];
CFRunLoopRef loop = CFRunLoopGetCurrent();
keyFilter = [KeyFilter alloc];
if (![keyFilter initWithRunLoop:loop]) {
NSRunAlertPanel(@"Failure", @"Failed to create event tap", @"Dammit", nil, nil);
return;
}
[self enableKeyFilter];
}
- (void) dealloc {
[keyFilter release];
[statusItem release];
[itemEnable release];
[itemConsole release];
[itemQuit release];
[console release];
[consoleWindow release];
[super dealloc];
}
@end