首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >keyDown和NSTableViewCocoa

keyDown和NSTableViewCocoa
EN

Stack Overflow用户
提问于 2012-02-16 22:39:20
回答 1查看 224关注 0票数 0

我有一个Cocoa应用程序,它从NSScrollView获取输入,然后想要输出一些关于按下的键的信息,比如keyCode、修饰符等。

问题是我在运行时得到了一个EXC_BAD_ACCESS错误。在运行之前没有编译器错误。

程序代码如下所示:

keyboardModel.h

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#import <Cocoa/Cocoa.h>

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

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

@end

MyController.m

代码语言:javascript
复制
#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

代码语言:javascript
复制
#import <Cocoa/Cocoa.h>

@interface MyTextView : NSTextView
{
   IBOutlet id controller;

}
@end

MyTextView.m

代码语言:javascript
复制
#import "MyTextView.h"
#import "MyController.h"

@implementation MyTextView

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

        @end

很抱歉这么长的缠绕代码,但这个错误快把我逼疯了。谢谢你的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-29 13:44:37

解决后,另一个类中的初始化器方法出现问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9313201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档