source: trunk/Cocoa/Pester/Source/NJRHotKey.m@ 131

Last change on this file since 131 was 131, checked in by Nicholas Riley, 21 years ago

PSPreferencesController.[hm]: Added support for registering hot keys;
not the most elegant thing in the world, but much better than it was
in the prototype. Triggered by +readPreferences.

NJRHotKeyField.[hm]: Replaced model components (wow, was that ever
dumb) by NJRHotKey reference, eliminating cumbersome archiving model.
Added accessors for hot key.

NJRHotKeyManager.[hm]: Ported Quentin Carnicelli's HotKeyCenter code
to use NJRHotKey, cleaned up, and removed reverse-engineered pre-10.2
support.

NJRHotKey.[hm]: New. Provides Cocoa-centric storage for
three-component hot keys, mapping from Cocoa to Carbon modifiers.

PSApplication.m: Reorganized. Added invocation of
+[PSPreferencesController readPreferences].

Fixes bug 29.

File size: 2.8 KB
Line 
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
15static NSString * const PLCharacters = @"characters"; // NSString
16static NSString * const PLModifierFlags = @"modifierFlags"; // NSNumber
17static 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}
42
43#pragma mark accessing
44
45- (NSString *)characters;
46{
47 return hotKeyCharacters;
48}
49
50- (unsigned)modifierFlags;
51{
52 return hotKeyModifierFlags;
53}
54
55- (long)modifiers;
56{
57 static long modifierMap[5][2] = {
58 { NSCommandKeyMask, cmdKey },
59 { NSAlternateKeyMask, optionKey },
60 { NSControlKeyMask, controlKey },
61 { NSShiftKeyMask, shiftKey },
62 { 0, 0 }
63 };
64
65 long modifiers = 0;
66 int i;
67
68 for( i = 0 ; modifierMap[i][0] != 0 ; i++ )
69 if( hotKeyModifierFlags & modifierMap[i][0] )
70 modifiers |= modifierMap[i][1];
71
72 return modifiers;
73}
74
75- (unsigned short)keyCode;
76{
77 return hotKeyCode;
78}
79
80#pragma mark property list serialization (Pester 1.1)
81
82- (NSDictionary *)propertyListRepresentation;
83{
84 return [NSDictionary dictionaryWithObjectsAndKeys:
85 hotKeyCharacters, PLCharacters,
86 [NSNumber numberWithUnsignedInt: hotKeyModifierFlags], PLModifierFlags,
87 [NSNumber numberWithUnsignedShort: hotKeyCode], PLKeyCode,
88 nil];
89}
90
91- (id)initWithPropertyList:(NSDictionary *)dict;
92{
93 if ( (self = [self init]) != nil) {
94 NS_DURING
95 hotKeyCharacters = [[dict objectForKey: PLCharacters] retain];
96 hotKeyModifierFlags = [[dict objectForKey: PLModifierFlags] unsignedIntValue];
97 hotKeyCode = [[dict objectForKey: PLKeyCode] unsignedShortValue];
98 NS_HANDLER
99 NS_ENDHANDLER
100 if (hotKeyCharacters == nil || hotKeyCode == 0) {
101 [self release];
102 self = nil;
103 }
104 }
105 return self;
106}
107
108- (NSString *)keyGlyphs;
109{
110 return [[hotKeyCharacters keyEquivalentAttributedStringWithModifierFlags: hotKeyModifierFlags] string];
111}
112
113@end
Note: See TracBrowser for help on using the repository browser.