Changeset 130 for trunk/Cocoa/Pester/Source/NJRHotKeyField.m
- Timestamp:
- 04/01/03 16:51:39 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Cocoa/Pester/Source/NJRHotKeyField.m
r129 r130 11 11 #import "NSString-NJRExtensions.h" 12 12 13 @implementation NJRHotKeyField 13 // property list keys 14 static NSString * const PLCharacters = @"characters"; // NSString 15 static NSString * const PLModifierFlags = @"modifierFlags"; // NSNumber 16 static NSString * const PLKeyCode = @"keyCode"; // NSNumber 14 17 15 const NSRange zeroRange = {0, 0}; 18 static const NSRange zeroRange = {0, 0}; 19 static const unsigned int capturedModifierMask = (NSShiftKeyMask | 20 NSControlKeyMask | 21 NSAlternateKeyMask | 22 NSCommandKeyMask); 23 16 24 static NSParagraphStyle *leftAlignStyle = nil, *centerAlignStyle = nil; 17 25 static NSDictionary *statusAttributes = nil; 26 27 @implementation NJRHotKeyField 18 28 19 29 + (void)initialize; … … 79 89 return; 80 90 } 81 NSMutableAttributedString *equivString = [[hotKeyCharacters keyEquivalentAttributedStringWithModifier Mask: hotKeyModifierFlags] mutableCopy];91 NSMutableAttributedString *equivString = [[hotKeyCharacters keyEquivalentAttributedStringWithModifierFlags: hotKeyModifierFlags] mutableCopy]; 82 92 [equivString addAttribute: NSParagraphStyleAttributeName 83 93 value: (finalized ? centerAlignStyle : leftAlignStyle) … … 88 98 } 89 99 90 - (void) setHotKeyEvent:(NSEvent *)theEvent;100 - (void)clearHotKey; 91 101 { 92 [hotKeyCharacters release]; hotKeyCharacters = [[theEvent charactersIgnoringModifiers] retain]; 93 hotKeyModifierFlags = [theEvent modifierFlags]; 94 hotKeyCode = [theEvent keyCode]; 102 [hotKeyCharacters release]; 103 hotKeyCharacters = nil; 104 hotKeyModifierFlags = 0; 105 hotKeyCode = 0; 106 [NSApp sendAction: [self action] to: [self target] from: self]; 95 107 } 96 108 … … 98 110 { 99 111 NSString *characters = [theEvent charactersIgnoringModifiers]; 100 if ([characters length] > 1) { 112 int length = [characters length]; 113 if (length > 1) { 101 114 [self showStatus: @"please press only one non-modifier key"]; 102 115 return; 103 116 } 104 // Yay, we can ask for keyCode, which is likely to be the same as Carbon's, I hope. 105 [self setHotKeyEvent: theEvent]; 106 107 [self showKeyEquivalentAttributedStringFinalized: ([theEvent modifierFlags] == 0)]; 117 if (length == 1) { 118 unsigned modifierFlags = ([theEvent modifierFlags] & capturedModifierMask); 119 id delegate = [self delegate]; 120 NSString *message = nil; 121 if (delegate != nil && ![delegate hotKeyField: self shouldAcceptCharacter: [characters characterAtIndex: 0] modifierFlags: modifierFlags rejectionMessage: &message]) { 122 [self showStatus: message != nil ? message : @"key is unavailable for use"]; 123 } else { 124 [hotKeyCharacters release]; 125 hotKeyCharacters = [characters retain]; 126 hotKeyModifierFlags = modifierFlags; 127 hotKeyCode = [theEvent keyCode]; 128 [NSApp sendAction: [self action] to: [self target] from: self]; 129 [self showKeyEquivalentAttributedStringFinalized: ([theEvent modifierFlags] & capturedModifierMask) == 0]; 130 } 131 } 108 132 } 109 133 … … 118 142 unsigned modifierFlags = [theEvent modifierFlags]; 119 143 120 // XXX why does my API call it a modifier mask when NSEvent's API calls it modifier flags? Check HostLauncher for usage. 121 122 if (modifierFlags == 0) { 144 if ((modifierFlags & capturedModifierMask) == 0) { 123 145 [self showKeyEquivalentAttributedStringFinalized: YES]; 124 146 } else { 125 147 [self previewKeyEquivalentAttributedString: 126 [@"" keyEquivalentAttributedStringWithModifier Mask: modifierFlags]];148 [@"" keyEquivalentAttributedStringWithModifierFlags: modifierFlags]]; 127 149 } 128 150 } … … 130 152 - (IBAction)clear:(id)sender; 131 153 { 132 [hotKeyCharacters release]; hotKeyCharacters = 0; 133 hotKeyModifierFlags = 0; 134 hotKeyCode = 0; 154 [self clearHotKey]; 135 155 [self showKeyEquivalentAttributedStringFinalized: YES]; 136 156 } 137 157 158 #pragma mark property list serialization (Pester 1.1) 159 160 - (NSDictionary *)propertyListRepresentation; 161 { 162 return [NSDictionary dictionaryWithObjectsAndKeys: 163 hotKeyCharacters, PLCharacters, 164 [NSNumber numberWithUnsignedInt: hotKeyModifierFlags], PLModifierFlags, 165 [NSNumber numberWithUnsignedShort: hotKeyCode], PLKeyCode, 166 nil]; 167 } 168 169 - (void)setFromPropertyList:(NSDictionary *)dict; 170 { 171 NS_DURING 172 hotKeyCharacters = [[dict objectForKey: PLCharacters] retain]; 173 hotKeyModifierFlags = [[dict objectForKey: PLModifierFlags] unsignedIntValue]; 174 hotKeyCode = [[dict objectForKey: PLKeyCode] unsignedShortValue]; 175 [self showKeyEquivalentAttributedStringFinalized: ([[NSApp currentEvent] modifierFlags] & capturedModifierMask) == 0]; 176 NS_HANDLER 177 [self clear: nil]; 178 NS_ENDHANDLER 179 } 180 138 181 @end
Note:
See TracChangeset
for help on using the changeset viewer.