source: trunk/Cocoa/X10Remote/NJRHotKey.m @ 678

Last change on this file since 678 was 157, checked in by Nicholas Riley, 17 years ago

Initial import.

File size: 2.6 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
11#include <Carbon/Carbon.h>
12
13// property list keys
14static NSString * const PLCharacters = @"characters"; // NSString
15static NSString * const PLModifierFlags = @"modifierFlags"; // NSNumber
16static NSString * const PLKeyCode = @"keyCode"; // NSNumber
17
18@implementation NJRHotKey
19
20#pragma mark initialize-release
21
22+ (NJRHotKey *)hotKeyWithCharacters:(NSString *)characters modifierFlags:(unsigned)modifierFlags keyCode:(unsigned short)keyCode;
23{
24    return [[[self alloc] initWithCharacters: characters modifierFlags: modifierFlags keyCode: keyCode] autorelease];
25}
26
27- (id)initWithCharacters:(NSString *)characters modifierFlags:(unsigned)modifierFlags keyCode:(unsigned short)keyCode;
28{
29    if ( (self = [self init]) != nil) {
30        hotKeyCharacters = [characters retain];
31        hotKeyModifierFlags = modifierFlags;
32        hotKeyCode = keyCode;
33    }
34    return self;
35}
36
37- (void)dealloc;
38{
39    [hotKeyCharacters release];
40}
41
42#pragma mark accessing
43
44- (NSString *)characters;
45{
46    return hotKeyCharacters;
47}
48
49- (unsigned)modifierFlags;
50{
51    return hotKeyModifierFlags;
52}
53
54- (long)modifiers;
55{
56    static long modifierMap[5][2] = {
57       { NSCommandKeyMask, cmdKey },
58       { NSAlternateKeyMask, optionKey },
59       { NSControlKeyMask, controlKey },
60       { NSShiftKeyMask, shiftKey },
61       { 0, 0 }
62    };
63
64    long modifiers = 0;
65    int i;
66
67    for (i = 0 ; modifierMap[i][0] != 0 ; i++)
68        if (hotKeyModifierFlags & modifierMap[i][0])
69            modifiers |= modifierMap[i][1];
70
71    return modifiers;
72}
73
74- (unsigned short)keyCode;
75{
76    return hotKeyCode;
77}
78
79#pragma mark property list serialization (Pester 1.1)
80
81- (NSDictionary *)propertyListRepresentation;
82{
83    return [NSDictionary dictionaryWithObjectsAndKeys:
84        hotKeyCharacters, PLCharacters,
85        [NSNumber numberWithUnsignedInt: hotKeyModifierFlags], PLModifierFlags,
86        [NSNumber numberWithUnsignedShort: hotKeyCode], PLKeyCode,
87        nil];
88}
89
90- (id)initWithPropertyList:(NSDictionary *)dict;
91{
92    if ( (self = [self init]) != nil) {
93        NS_DURING
94            hotKeyCharacters = [[dict objectForKey: PLCharacters] retain];
95            hotKeyModifierFlags = [[dict objectForKey: PLModifierFlags] unsignedIntValue];
96            hotKeyCode = [[dict objectForKey: PLKeyCode] unsignedShortValue];
97        NS_HANDLER
98        NS_ENDHANDLER
99        if (hotKeyCharacters == nil || hotKeyCode == 0) {
100            [self release];
101            self = nil;
102        }
103    }
104    return self;
105}
106
107@end
Note: See TracBrowser for help on using the repository browser.