1 | //
|
---|
2 | // NJRHotKey.m
|
---|
3 | // Pester
|
---|
4 | //
|
---|
5 | // Created by Nicholas Riley on Tue Apr 01 2003.
|
---|
6 | // Copyright (c) 2003 Nicholas Riley. All rights reserved.
|
---|
7 | //
|
---|
8 |
|
---|
9 | #import "NJRHotKey.h"
|
---|
10 | #import "NSString-NJRExtensions.h"
|
---|
11 |
|
---|
12 | #include <Carbon/Carbon.h>
|
---|
13 |
|
---|
14 | // property list keys
|
---|
15 | static NSString * const PLCharacters = @"characters"; // NSString
|
---|
16 | static NSString * const PLModifierFlags = @"modifierFlags"; // NSNumber
|
---|
17 | static NSString * const PLKeyCode = @"keyCode"; // NSNumber
|
---|
18 |
|
---|
19 | @implementation NJRHotKey
|
---|
20 |
|
---|
21 | #pragma mark initialize-release
|
---|
22 |
|
---|
23 | + (NJRHotKey *)hotKeyWithCharacters:(NSString *)characters modifierFlags:(unsigned)modifierFlags keyCode:(unsigned short)keyCode;
|
---|
24 | {
|
---|
25 | return [[[self alloc] initWithCharacters: characters modifierFlags: modifierFlags keyCode: keyCode] autorelease];
|
---|
26 | }
|
---|
27 |
|
---|
28 | - (id)initWithCharacters:(NSString *)characters modifierFlags:(unsigned)modifierFlags keyCode:(unsigned short)keyCode;
|
---|
29 | {
|
---|
30 | if ( (self = [self init]) != nil) {
|
---|
31 | hotKeyCharacters = [characters retain];
|
---|
32 | hotKeyModifierFlags = modifierFlags;
|
---|
33 | hotKeyCode = keyCode;
|
---|
34 | }
|
---|
35 | return self;
|
---|
36 | }
|
---|
37 |
|
---|
38 | - (void)dealloc;
|
---|
39 | {
|
---|
40 | [hotKeyCharacters release];
|
---|
41 | [super dealloc];
|
---|
42 | }
|
---|
43 |
|
---|
44 | #pragma mark accessing
|
---|
45 |
|
---|
46 | - (NSString *)characters;
|
---|
47 | {
|
---|
48 | return hotKeyCharacters;
|
---|
49 | }
|
---|
50 |
|
---|
51 | - (unsigned)modifierFlags;
|
---|
52 | {
|
---|
53 | return hotKeyModifierFlags;
|
---|
54 | }
|
---|
55 |
|
---|
56 | - (long)modifiers;
|
---|
57 | {
|
---|
58 | static long modifierMap[5][2] = {
|
---|
59 | { NSCommandKeyMask, cmdKey },
|
---|
60 | { NSAlternateKeyMask, optionKey },
|
---|
61 | { NSControlKeyMask, controlKey },
|
---|
62 | { NSShiftKeyMask, shiftKey },
|
---|
63 | { 0, 0 }
|
---|
64 | };
|
---|
65 |
|
---|
66 | long modifiers = 0;
|
---|
67 | int i;
|
---|
68 |
|
---|
69 | for (i = 0 ; modifierMap[i][0] != 0 ; i++)
|
---|
70 | if (hotKeyModifierFlags & modifierMap[i][0])
|
---|
71 | modifiers |= modifierMap[i][1];
|
---|
72 |
|
---|
73 | return modifiers;
|
---|
74 | }
|
---|
75 |
|
---|
76 | - (unsigned short)keyCode;
|
---|
77 | {
|
---|
78 | return hotKeyCode;
|
---|
79 | }
|
---|
80 |
|
---|
81 | #pragma mark property list serialization (Pester 1.1)
|
---|
82 |
|
---|
83 | - (NSDictionary *)propertyListRepresentation;
|
---|
84 | {
|
---|
85 | return [NSDictionary dictionaryWithObjectsAndKeys:
|
---|
86 | hotKeyCharacters, PLCharacters,
|
---|
87 | [NSNumber numberWithUnsignedInt: hotKeyModifierFlags], PLModifierFlags,
|
---|
88 | [NSNumber numberWithUnsignedShort: hotKeyCode], PLKeyCode,
|
---|
89 | nil];
|
---|
90 | }
|
---|
91 |
|
---|
92 | - (id)initWithPropertyList:(NSDictionary *)dict;
|
---|
93 | {
|
---|
94 | if ( (self = [self init]) != nil) {
|
---|
95 | @try {
|
---|
96 | hotKeyCharacters = [[dict objectForKey: PLCharacters] retain];
|
---|
97 | hotKeyModifierFlags = [[dict objectForKey: PLModifierFlags] unsignedIntValue];
|
---|
98 | hotKeyCode = [[dict objectForKey: PLKeyCode] unsignedShortValue];
|
---|
99 | } @catch (NSException *exception) {
|
---|
100 | }
|
---|
101 | if (hotKeyCharacters == nil || hotKeyCode == 0) {
|
---|
102 | [self release];
|
---|
103 | self = nil;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | return self;
|
---|
107 | }
|
---|
108 |
|
---|
109 | - (NSString *)keyGlyphs;
|
---|
110 | {
|
---|
111 | return [[hotKeyCharacters keyEquivalentAttributedStringWithModifierFlags: hotKeyModifierFlags] string];
|
---|
112 | }
|
---|
113 |
|
---|
114 | @end
|
---|