2012-02-16 25 views
0

我有一個Cocoa應用程序,從NSScrollView獲取輸入,然後想輸出壓在關鍵的一些信息,比如鍵代碼,修改等KeyDown和NSTableViewCocoa

的問題是,我得到的運行時出現EXC_BAD_ACCESS錯誤。運行前沒有編譯器錯誤。

本方案的代碼被列舉如下:

keyboardModel.h

#import <Cocoa/Cocoa.h> 


@interface keyboardModel : NSObject { 

    NSString* charac; 
    NSString* keyc; 
    NSString* modif; 

} 


-(void) setValues: (NSString *) a :(NSString *)b :(NSString *) c; 

@property (copy) NSString* charac; 
@property (copy) NSString* keyc; 
@property (copy) NSString* modif; 

@end 

keyboardModel.m

#import "keyboardModel.h" 
#import "MyController.h" 


@implementation keyboardModel 

@synthesize charac; 
@synthesize keyc; 
@synthesize modif; 

-(void) setValues: (NSString *) a :(NSString *)b :(NSString *) c{ 

    charac = [charac stringByAppendingString:a]; 
    keyc = [keyc stringByAppendingString:b]; 
    modif = [modif stringByAppendingString:c]; 

} 

-(id) init { 
     self = [super init]; 

    if (self) 
    {  
     charac = @"dddd"; 
     keyc = @"xxx"; 
     modif = @"xxx"; 
    } 

    return self; 
} 


@end 

MyController.h

#import <Cocoa/Cocoa.h> 

@interface MyController : NSObject<NSTableViewDataSource> 
{ 
    IBOutlet id typingArea; 
    IBOutlet NSTableView *tableView; 
    NSMutableArray *list; 
} 

-(void)showKeyDownEvent:(NSEvent *)theEvent; 

@end 

MyController.m

#import "MyController.h" 
#import "MyTextView.h" 
#import "keyboardModel.h" 

#import <Carbon/Carbon.h> 

static const int INS_MOD_FLAG_OPTION_KEY = (optionKey >> 8) & 0xff; 
static const int INS_MOD_FLAG_SHIFT_KEY = (shiftKey >> 8) & 0xff; 
static const int INS_MOD_FLAG_CONTROL_KEY = (controlKey >> 8) & 0xff; 
static const int INS_MOD_FLAG_ALPHA_LOCK = (alphaLock >> 8) & 0xff; 
static const int INS_MOD_FLAG_CMD_KEY = (cmdKey >> 8) & 0xff; 

@implementation MyController 

- (id) init 
{ 
     self = [super init]; 
    if (self) 
    { 
     list = [[NSMutableArray alloc] init]; 

    } 
    return self; 
} 


static NSString* print_mods(UInt32 unl_mods) { 

    NSString *modifiers = [NSString stringWithFormat:@""]; 

    if(unl_mods & NSAlphaShiftKeyMask) 

     modifiers = [NSString stringWithFormat:@"%@ Caps Lock", modifiers]; 

    if(unl_mods & NSShiftKeyMask) 

     modifiers = [NSString stringWithFormat:@"%@ Shift Key", modifiers]; 

    if(unl_mods & NSControlKeyMask) 

     modifiers = [NSString stringWithFormat:@"%@ Control Key", modifiers]; 

    if(unl_mods & NSAlternateKeyMask) 

     modifiers = [NSString stringWithFormat:@"%@ Alt Key", modifiers]; 

    if (unl_mods == 384) 

     modifiers = [NSString stringWithFormat:@"No Modifier"]; 


    return modifiers; 
} 

-(void)showKeyDownEvent:(NSEvent *)e 
{ 

    //%x - hex value 
    //%d - decimal value 

    NSString* myNewString1 = [NSString stringWithFormat:@"%x(%d)", [e keyCode], [e keyCode]]; 
    NSString* myNewString2 = [NSString stringWithFormat:@"%d", [e modifierFlags]]; 


    NSString* flagReplace = [NSString stringWithFormat:@""]; 
    int value = [myNewString2 intValue]; 

    NSLog (@"%d",value); 

    flagReplace = print_mods ([e modifierFlags]); 

    NSString *temp1 = [@"Character: " stringByAppendingString: [e characters]]; 
    NSString *temp2 = [@" Keycode: " stringByAppendingString: myNewString1]; 
    NSString *temp3 = [@" Modifier: " stringByAppendingString: flagReplace]; 


    keyboardModel *km = [[keyboardModel alloc] init]; 
    [km setValues:temp1:temp2:temp3]; 
    [list addObject: km]; 

    [tableView reloadData]; 
} 

- (id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{ 
    keyboardModel *p = [list objectAtIndex:row]; 
    NSString* identifier = [tableColumn identifier]; 

    return [p valueForKey: identifier]; 
} 

-(NSInteger) numberOfRowsInTableView : (NSTableView *) tableView { 
    return [list count]; 
} 

// closing the last window quits the app 
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication 
{ 
    return YES; 
} 

@end 

MyTextView.h

#import <Cocoa/Cocoa.h> 

@interface MyTextView : NSTextView 
{ 
    IBOutlet id controller; 

} 
@end 

MyTextView.m

#import "MyTextView.h" 
#import "MyController.h" 

@implementation MyTextView 

- (void)keyDown:(NSEvent *)theEvent 
{ 
    [controller showKeyDownEvent: theEvent]; 
    [super keyDown: theEvent]; 
} 

     @end 

很抱歉的長期曲折的代碼,但這個錯誤我發瘋。謝謝你的幫助!

回答

0

解決了,在另一個類中存在初始化方法的問題。