Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7998551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:14:30+00:00 2026-06-04T15:14:30+00:00

I have a Cocoa application that gets the input from an NSScrollView and then

  • 0

I have a Cocoa application that gets the input from an NSScrollView and then wants to output some information on the key pressed, such as the keyCode, modifier etc.

The problem is that I am getting an EXC_BAD_ACCESS error at runtime. There are no compiler errors before runtime.

The code for the program is listed below:

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

Sorry for the long winding code, but the error is driving me nuts. Thanks for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:14:32+00:00Added an answer on June 4, 2026 at 3:14 pm

    Solved, there was a problem with the initializer method in another class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a cocoa application, that on pressing a certain key gets selected text
I have an Cocoa Application (Mac OS X SDK 10.7) that is performing some
I have a Cocoa application that records datestamps on events. I need to know
I have a Cocoa application that, periodically, needs to install pkg X onto the
I've got a small Cocoa problem. I have a StatusBar application that has an
I have a Cocoa, Document-based Mac OS X application. One feature that I have
I have a Cocoa application (.app) and I would like to launch it from
I have a cocoa application that has a finder like feel it and is
In my cocoa application I have a WebView. If I set that WebView to
I have customized the contextual menu for my Cocoa application such that only certain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.