added possibility to change hotkey

This commit is contained in:
Jeena Paradies 2010-05-01 15:12:18 +02:00
parent c94e5af643
commit 543e4e99eb
3 changed files with 285 additions and 24 deletions

View file

@ -17,6 +17,7 @@
IBOutlet NSWindow *timelineViewWindow; IBOutlet NSWindow *timelineViewWindow;
IBOutlet WebView *mentionsView; IBOutlet WebView *mentionsView;
IBOutlet NSWindow *mentionsViewWindow; IBOutlet NSWindow *mentionsViewWindow;
IBOutlet NSMenuItem *globalHotkeyMenuItem;
ViewDelegate *viewDelegate; ViewDelegate *viewDelegate;
} }
@ -24,14 +25,17 @@
@property (retain, nonatomic) IBOutlet NSWindow *timelineViewWindow; @property (retain, nonatomic) IBOutlet NSWindow *timelineViewWindow;
@property (retain, nonatomic) IBOutlet WebView *mentionsView; @property (retain, nonatomic) IBOutlet WebView *mentionsView;
@property (retain, nonatomic) IBOutlet NSWindow *mentionsViewWindow; @property (retain, nonatomic) IBOutlet NSWindow *mentionsViewWindow;
@property (retain, nonatomic) IBOutlet NSMenuItem *globalHotkeyMenuItem;
@property (retain, nonatomic) IBOutlet ViewDelegate *viewDelegate; @property (retain, nonatomic) IBOutlet ViewDelegate *viewDelegate;
- (void)initWebViews; - (void)initWebViews;
- (void)initHotKeys;
- (void)openNewTweetWindowInReplyTo:(NSString *)userName statusId:(NSString *)statusId; - (void)openNewTweetWindowInReplyTo:(NSString *)userName statusId:(NSString *)statusId;
- (NSString *)pluginURL; - (NSString *)pluginURL;
- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent; - (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent;
- (void)unreadMentions:(NSInteger)count; - (void)unreadMentions:(NSInteger)count;
OSStatus handler(EventHandlerCallRef nextHandler, EventRef theEvent, void* userData); OSStatus handler(EventHandlerCallRef nextHandler, EventRef theEvent, void* userData);
+ (NSString *)stringFromVirtualKeyCode:(NSInteger)code;
@end @end

View file

@ -12,27 +12,11 @@
@implementation Controller @implementation Controller
@synthesize timelineView, timelineViewWindow, mentionsView, mentionsViewWindow, viewDelegate; @synthesize timelineView, timelineViewWindow, mentionsView, mentionsViewWindow, globalHotkeyMenuItem, viewDelegate;
- (void)awakeFromNib { - (void)awakeFromNib {
[self initWebViews]; [self initWebViews];
[self initHotKeys];
/* CARBON from http://github.com/Xjs/drama-button/blob/carbon/Drama_ButtonAppDelegate.m */
EventTypeSpec eventType;
eventType.eventClass = kEventClassKeyboard;
eventType.eventKind = kEventHotKeyPressed;
InstallApplicationEventHandler(&handler, 1, &eventType, NULL, NULL);
EventHotKeyID g_HotKeyID;
g_HotKeyID.id = 1;
EventHotKeyRef g_HotKeyRef;
RegisterEventHotKey(17, controlKey + cmdKey + optionKey, g_HotKeyID, GetApplicationEventTarget(), 0, &g_HotKeyRef);
/* end CARBON */
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self [nc addObserver:self
@ -51,6 +35,68 @@
andEventID:kAEGetURL]; andEventID:kAEGetURL];
} }
- (void)initHotKeys {
NSInteger newTweetKey = kVK_ANSI_T; // HIToolbox/Events.h
NSInteger newTweetModifierKey = controlKey + cmdKey + optionKey; // controlKey 4096, cmdKey 256, optionKey 2048
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSInteger defaultsNewTweetKey = (NSInteger)[defaults integerForKey:@"newTweetKey"];
if ([NSNumber numberWithInt:defaultsNewTweetKey] != nil) {
newTweetKey = defaultsNewTweetKey;
} else {
[defaults setInteger:newTweetKey forKey:@"newTweetKey"];
}
NSInteger defaultsNewTweetModifierKey = (NSInteger)[defaults integerForKey:@"newTweetModifierKey"];
if ([NSNumber numberWithInt:defaultsNewTweetModifierKey] != nil) {
newTweetModifierKey = defaultsNewTweetModifierKey;
} else {
[defaults setInteger:newTweetModifierKey forKey:@"newTweetModifierKey"];
}
NSUInteger cocoaModifiers = 0;
if (newTweetModifierKey & shiftKey) cocoaModifiers = cocoaModifiers | NSShiftKeyMask;
if (newTweetModifierKey & optionKey) cocoaModifiers = cocoaModifiers | NSAlternateKeyMask;
if (newTweetModifierKey & controlKey) cocoaModifiers = cocoaModifiers | NSControlKeyMask;
if (newTweetModifierKey & cmdKey) cocoaModifiers = cocoaModifiers | NSCommandKeyMask;
NSLog(@"%i", NSShiftKeyMask);
NSInteger theNumber = cocoaModifiers;
NSMutableString *str = [NSMutableString string];
NSInteger numberCopy = theNumber; // so you won't change your original value
for(NSInteger i = 0; i < 32 ; i++) {
// Prepend "0" or "1", depending on the bit
[str insertString:((numberCopy & 1) ? @"1" : @"0") atIndex:0];
numberCopy >>= 1;
}
// NSLog(@"Binary version: %@", str);
NSLog(@"%c", kVK_ANSI_T);
[globalHotkeyMenuItem setKeyEquivalent:[Controller stringFromVirtualKeyCode:newTweetKey]];
[globalHotkeyMenuItem setKeyEquivalentModifierMask:cocoaModifiers];
/* CARBON from http://github.com/Xjs/drama-button/blob/carbon/Drama_ButtonAppDelegate.m */
EventTypeSpec eventType;
eventType.eventClass = kEventClassKeyboard;
eventType.eventKind = kEventHotKeyPressed;
InstallApplicationEventHandler(&handler, 1, &eventType, NULL, NULL);
EventHotKeyID g_HotKeyID;
g_HotKeyID.id = 1;
EventHotKeyRef g_HotKeyRef;
RegisterEventHotKey(newTweetKey, newTweetModifierKey, g_HotKeyID, GetApplicationEventTarget(), 0, &g_HotKeyRef);
/* end CARBON */
}
- (void)initWebViews { - (void)initWebViews {
NSString *path = [[NSBundle mainBundle] resourcePath]; NSString *path = [[NSBundle mainBundle] resourcePath];
@ -135,6 +181,211 @@
} }
} }
+ (NSString *)stringFromVirtualKeyCode:(NSInteger)code {
NSString *string;
switch (code) {
case kVK_ANSI_A:
string = @"A";
break;
case kVK_ANSI_S:
string = @"S";
break;
case kVK_ANSI_D:
string = @"D";
break;
case kVK_ANSI_F:
string = @"F";
break;
case kVK_ANSI_H:
string = @"H";
break;
case kVK_ANSI_G:
string = @"G";
break;
case kVK_ANSI_Z:
string = @"Z";
break;
case kVK_ANSI_X:
string = @"X";
break;
case kVK_ANSI_C:
string = @"C";
break;
case kVK_ANSI_V:
string = @"V";
break;
case kVK_ANSI_B:
string = @"B";
break;
case kVK_ANSI_Q:
string = @"Q";
break;
case kVK_ANSI_W:
string = @"W";
break;
case kVK_ANSI_E:
string = @"E";
break;
case kVK_ANSI_R:
string = @"R";
break;
case kVK_ANSI_Y:
string = @"Y";
break;
case kVK_ANSI_T:
string = @"T";
break;
case kVK_ANSI_1:
string = @"1";
break;
case kVK_ANSI_2:
string = @"2";
break;
case kVK_ANSI_3:
string = @"3";
break;
case kVK_ANSI_4:
string = @"4";
break;
case kVK_ANSI_6:
string = @"6";
break;
case kVK_ANSI_5:
string = @"5";
break;
case kVK_ANSI_Equal:
string = @"=";
break;
case kVK_ANSI_9:
string = @"9";
break;
case kVK_ANSI_7:
string = @"7";
break;
case kVK_ANSI_Minus:
string = @"-";
break;
case kVK_ANSI_8:
string = @"8";
break;
case kVK_ANSI_0:
string = @"0";
break;
case kVK_ANSI_RightBracket:
string = @")";
break;
case kVK_ANSI_O:
string = @"0";
break;
case kVK_ANSI_U:
string = @"U";
break;
case kVK_ANSI_LeftBracket:
string = @"(";
break;
case kVK_ANSI_I:
string = @"I";
break;
case kVK_ANSI_P:
string = @"P";
break;
case kVK_ANSI_L:
string = @"L";
break;
case kVK_ANSI_J:
string = @"J";
break;
case kVK_ANSI_Quote:
string = @"\"";
break;
case kVK_ANSI_K:
string = @"K";
break;
case kVK_ANSI_Semicolon:
string = @";";
break;
case kVK_ANSI_Backslash:
string = @"\\";
break;
case kVK_ANSI_Comma:
string = @",";
break;
case kVK_ANSI_Slash:
string = @"/";
break;
case kVK_ANSI_N:
string = @"N";
break;
case kVK_ANSI_M:
string = @"M";
break;
case kVK_ANSI_Period:
string = @".";
break;
case kVK_ANSI_Grave:
string = @"`";
break;
case kVK_ANSI_KeypadDecimal:
string = @".";
break;
case kVK_ANSI_KeypadMultiply:
string = @"*";
break;
case kVK_ANSI_KeypadPlus:
string = @"+";
break;
case kVK_ANSI_KeypadClear:
string = @"";
break;
case kVK_ANSI_KeypadDivide:
string = @"/";
break;
case kVK_ANSI_KeypadEnter:
string = @"⎆";
break;
case kVK_ANSI_KeypadMinus:
string = @"-";
break;
case kVK_ANSI_KeypadEquals:
string = @"=";
break;
case kVK_ANSI_Keypad0:
string = @"0";
break;
case kVK_ANSI_Keypad1:
string = @"1";
break;
case kVK_ANSI_Keypad2:
string = @"2";
break;
case kVK_ANSI_Keypad3:
string = @"3";
break;
case kVK_ANSI_Keypad4:
string = @"4";
break;
case kVK_ANSI_Keypad5:
string = @"5";
break;
case kVK_ANSI_Keypad6:
string = @"6";
break;
case kVK_ANSI_Keypad7:
string = @"7";
break;
case kVK_ANSI_Keypad8:
string = @"8";
break;
case kVK_ANSI_Keypad9:
string = @"9";
break;
default:
break;
}
return string;
}
/* CARBON */ /* CARBON */

View file

@ -21,9 +21,9 @@
</object> </object>
<object class="NSMutableArray" key="IBDocument.EditedObjectIDs"> <object class="NSMutableArray" key="IBDocument.EditedObjectIDs">
<bool key="EncodedWithXMLCoder">YES</bool> <bool key="EncodedWithXMLCoder">YES</bool>
<integer value="559"/>
<integer value="81"/> <integer value="81"/>
<integer value="535"/> <integer value="535"/>
<integer value="559"/>
</object> </object>
<object class="NSArray" key="IBDocument.PluginDependencies"> <object class="NSArray" key="IBDocument.PluginDependencies">
<bool key="EncodedWithXMLCoder">YES</bool> <bool key="EncodedWithXMLCoder">YES</bool>
@ -1323,6 +1323,14 @@
</object> </object>
<int key="connectionID">569</int> <int key="connectionID">569</int>
</object> </object>
<object class="IBConnectionRecord">
<object class="IBOutletConnection" key="connection">
<string key="label">globalHotkeyMenuItem</string>
<reference key="source" ref="408500656"/>
<reference key="destination" ref="722745758"/>
</object>
<int key="connectionID">570</int>
</object>
</object> </object>
<object class="IBMutableOrderedSet" key="objectRecords"> <object class="IBMutableOrderedSet" key="objectRecords">
<object class="NSArray" key="orderedObjects"> <object class="NSArray" key="orderedObjects">
@ -2342,7 +2350,7 @@
</object> </object>
</object> </object>
<nil key="sourceID"/> <nil key="sourceID"/>
<int key="maxID">569</int> <int key="maxID">570</int>
</object> </object>
<object class="IBClassDescriber" key="IBDocument.Classes"> <object class="IBClassDescriber" key="IBDocument.Classes">
<object class="NSMutableArray" key="referencedPartialClassDescriptions"> <object class="NSMutableArray" key="referencedPartialClassDescriptions">
@ -2350,14 +2358,11 @@
<object class="IBPartialClassDescription"> <object class="IBPartialClassDescription">
<string key="className">Controller</string> <string key="className">Controller</string>
<string key="superclassName">NSObject</string> <string key="superclassName">NSObject</string>
<object class="NSMutableDictionary" key="actions">
<string key="NS.key.0">mentionsWindowDidExpose:</string>
<string key="NS.object.0">id</string>
</object>
<object class="NSMutableDictionary" key="outlets"> <object class="NSMutableDictionary" key="outlets">
<bool key="EncodedWithXMLCoder">YES</bool> <bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSArray" key="dict.sortedKeys"> <object class="NSArray" key="dict.sortedKeys">
<bool key="EncodedWithXMLCoder">YES</bool> <bool key="EncodedWithXMLCoder">YES</bool>
<string>globalHotkeyMenuItem</string>
<string>mentionsView</string> <string>mentionsView</string>
<string>mentionsViewWindow</string> <string>mentionsViewWindow</string>
<string>timelineView</string> <string>timelineView</string>
@ -2366,6 +2371,7 @@
</object> </object>
<object class="NSMutableArray" key="dict.values"> <object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool> <bool key="EncodedWithXMLCoder">YES</bool>
<string>NSMenuItem</string>
<string>WebView</string> <string>WebView</string>
<string>NSWindow</string> <string>NSWindow</string>
<string>WebView</string> <string>WebView</string>