-
Notifications
You must be signed in to change notification settings - Fork 4
/
NJRHotKey.m
97 lines (76 loc) · 2.15 KB
/
NJRHotKey.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
//
// NJRHotKey.m
// Pester
//
// Created by Nicholas Riley on Tue Apr 01 2003.
// Copyright (c) 2003 Nicholas Riley. All rights reserved.
//
#import "NJRHotKey.h"
#include <Carbon/Carbon.h>
#import <ShortcutRecorder/ShortcutRecorder.h>
@implementation NJRHotKey
#pragma mark initialize-release
+ (NJRHotKey *)noHotKey;
{
static NJRHotKey *noHotKey = nil;
if (noHotKey == nil)
noHotKey = [[self alloc] initWithCharacters: nil modifierFlags: 0 keyCode: 0xFFFF];
return noHotKey;
}
+ (NJRHotKey *)hotKeyWithCharacters:(NSString *)characters modifierFlags:(unsigned)modifierFlags keyCode:(unsigned short)keyCode;
{
return [[[self alloc] initWithCharacters: characters modifierFlags: modifierFlags keyCode: keyCode] autorelease];
}
- (id)initWithCharacters:(NSString *)characters modifierFlags:(unsigned)modifierFlags keyCode:(unsigned short)keyCode;
{
if ( (self = [self init]) != nil) {
hotKeyCharacters = [characters retain];
hotKeyModifierFlags = modifierFlags;
hotKeyCode = keyCode;
}
return self;
}
- (void)dealloc;
{
[hotKeyCharacters release];
[super dealloc];
}
#pragma mark accessing
- (NSString *)characters;
{
return hotKeyCharacters;
}
- (unsigned)modifierFlags;
{
return hotKeyModifierFlags;
}
- (UInt32)modifiers;
{
static UInt32 modifierMap[5][2] = {
{ NSCommandKeyMask, cmdKey },
{ NSAlternateKeyMask, optionKey },
{ NSControlKeyMask, controlKey },
{ NSShiftKeyMask, shiftKey },
{ 0, 0 }
};
UInt32 modifiers = 0;
int i;
for (i = 0 ; modifierMap[i][0] != 0 ; i++)
if (hotKeyModifierFlags & modifierMap[i][0])
modifiers |= modifierMap[i][1];
return modifiers;
}
- (unsigned short)keyCode;
{
return hotKeyCode;
}
- (NSString *)keyGlyphs;
{
return SRStringForCocoaModifierFlagsAndKeyCode(hotKeyModifierFlags, hotKeyCode);
}
- (NSString *)description;
{
if (hotKeyCharacters == nil) return [NSString stringWithFormat: @"<%@: none>", [self class]];
return [NSString stringWithFormat: @"<%@: %x + %@ (%d) = %@>", [self class], hotKeyModifierFlags, hotKeyCharacters, hotKeyCode, [self keyGlyphs]];
}
@end