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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
// Created by Allen Ingling on Fri Jun 18 2004.
// Copyright (c) 2004 New York University. All rights reserved.
#import "KeyRecorderWindow.h"
@implementation KeyRecorderWindow
- (id)init
{
if (self = [super init]) {
enableFlag=FALSE;
characterList=[[NSMutableArray alloc] initWithCapacity:1];
}
return(self);
}
- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag
{
if (self = [super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag]) {
enableFlag=FALSE;
characterList=[[NSMutableArray alloc] initWithCapacity:1];
}
return(self);
}
- (void)enableKeypressCollection
{
enableFlag=TRUE;
}
- (void)disableKeypressCollection
{
enableFlag=FALSE;
}
- (void)dealloc
{
if(characterList != NULL)
[characterList release];
[super dealloc];
}
/*
- (void)mouseDown:(NSEvent *)theEvent
{
int x;
x=100;
}
*/
- (void)keyDown:(NSEvent *)theEvent
{
NSString *keyCharacter;
NSNumber *timestamp, *tickCount, *keyCode;
NSDictionary *tempEntry, *modifierFlags;
unsigned int rawEventModifierFlags;
unsigned short keyCodeUShort;
if(enableFlag){
tickCount=[NSNumber numberWithDouble:(double)TickCount()];
keyCharacter=[theEvent characters];
keyCodeUShort=[theEvent keyCode];
keyCode=[NSNumber numberWithUnsignedShort:keyCodeUShort];
//key modifier flag constants are defined by Cocoa.h, inclusion of which in a mex file is barred by conflicts. So we transfer flags back to the
//mex file as key-value CF array rathern than in a numeric value.
//Note: if we cared about economizing on memory usage, then we could retain the packed flag value and generate the
//dictionary from that only when accessed from outside, such as via ReadNextChar.
//Note: also include the tickcount.
rawEventModifierFlags=[theEvent modifierFlags];
modifierFlags=[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:(BOOL)((rawEventModifierFlags & NSAlphaShiftKeyMask) ? YES : NO)], @"NSAlphaShiftKeyMask",
[NSNumber numberWithBool:(BOOL)((rawEventModifierFlags & NSShiftKeyMask) ? YES : NO)], @"NSShiftKeyMask",
[NSNumber numberWithBool:(BOOL)((rawEventModifierFlags & NSControlKeyMask) ? YES : NO)], @"NSControlKeyMask",
[NSNumber numberWithBool:(BOOL)((rawEventModifierFlags & NSAlternateKeyMask) ? YES : NO)], @"NSAlternateKeyMask",
[NSNumber numberWithBool:(BOOL)((rawEventModifierFlags & NSCommandKeyMask) ? YES : NO)], @"NSCommandKeyMask",
[NSNumber numberWithBool:(BOOL)((rawEventModifierFlags & NSNumericPadKeyMask) ? YES : NO)], @"NSNumericPadKeyMask",
[NSNumber numberWithBool:(BOOL)((rawEventModifierFlags & NSHelpKeyMask) ? YES : NO)], @"NSHelpKeyMask",
[NSNumber numberWithBool:(BOOL)((rawEventModifierFlags & NSFunctionKeyMask) ? YES : NO)], @"NSFunctionKeyMask",
nil];
timestamp=[NSNumber numberWithDouble:(double)[theEvent timestamp]];
tempEntry=[NSDictionary dictionaryWithObjectsAndKeys:
keyCharacter, @"character",
timestamp, @"time",
modifierFlags, @"modifierFlags",
tickCount, @"tickCount",
keyCode, @"keyCode",
nil];
[characterList addObject:tempEntry];
}
}
- (NSDictionary*)readNextChar
{
NSDictionary *charDictionary;
//return autoreleased and remove from the queue the first character.
if([characterList count] == 0)
return(NULL);
else{
charDictionary=[NSDictionary dictionaryWithDictionary:[characterList objectAtIndex:0]];
[characterList removeObjectAtIndex:0];
return(charDictionary);
}
}
- (NSDictionary*)peekNextChar
{
NSDictionary *charDictionary;
//return autoreleased and remove from the queue the first character.
if([characterList count] == 0)
return(NULL);
else{
charDictionary=[NSDictionary dictionaryWithDictionary:[characterList objectAtIndex:0]];
return(charDictionary);
}
}
- (NSArray*)readCharList
{
NSArray *tempCharacterList;
tempCharacterList= [NSArray arrayWithArray:characterList];
[characterList removeAllObjects];
return(tempCharacterList);
}
- (NSArray*)peekCharList
{
NSArray *tempCharacterList;
tempCharacterList= [NSArray arrayWithArray:characterList];
return(tempCharacterList);
}
- (void)clearCharList
{
[characterList removeAllObjects];
}
- (int)getCharListLength
{
return((int)[characterList count]);
}
@end
|