Note that there are some explanatory texts on larger screens.

plurals
  1. POkeyDown and NSTableViewCocoa
    text
    copied!<p>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.</p> <p>The problem is that I am getting an EXC_BAD_ACCESS error at runtime. There are no compiler errors before runtime.</p> <p>The code for the program is listed below:</p> <p>keyboardModel.h</p> <pre><code>#import &lt;Cocoa/Cocoa.h&gt; @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 </code></pre> <p>keyboardModel.m</p> <pre><code>#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 </code></pre> <p>MyController.h</p> <pre><code>#import &lt;Cocoa/Cocoa.h&gt; @interface MyController : NSObject&lt;NSTableViewDataSource&gt; { IBOutlet id typingArea; IBOutlet NSTableView *tableView; NSMutableArray *list; } -(void)showKeyDownEvent:(NSEvent *)theEvent; @end </code></pre> <p>MyController.m</p> <pre><code>#import "MyController.h" #import "MyTextView.h" #import "keyboardModel.h" #import &lt;Carbon/Carbon.h&gt; static const int INS_MOD_FLAG_OPTION_KEY = (optionKey &gt;&gt; 8) &amp; 0xff; static const int INS_MOD_FLAG_SHIFT_KEY = (shiftKey &gt;&gt; 8) &amp; 0xff; static const int INS_MOD_FLAG_CONTROL_KEY = (controlKey &gt;&gt; 8) &amp; 0xff; static const int INS_MOD_FLAG_ALPHA_LOCK = (alphaLock &gt;&gt; 8) &amp; 0xff; static const int INS_MOD_FLAG_CMD_KEY = (cmdKey &gt;&gt; 8) &amp; 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 &amp; NSAlphaShiftKeyMask) modifiers = [NSString stringWithFormat:@"%@ Caps Lock", modifiers]; if(unl_mods &amp; NSShiftKeyMask) modifiers = [NSString stringWithFormat:@"%@ Shift Key", modifiers]; if(unl_mods &amp; NSControlKeyMask) modifiers = [NSString stringWithFormat:@"%@ Control Key", modifiers]; if(unl_mods &amp; 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 </code></pre> <p>MyTextView.h</p> <pre><code>#import &lt;Cocoa/Cocoa.h&gt; @interface MyTextView : NSTextView { IBOutlet id controller; } @end </code></pre> <p>MyTextView.m</p> <pre><code>#import "MyTextView.h" #import "MyController.h" @implementation MyTextView - (void)keyDown:(NSEvent *)theEvent { [controller showKeyDownEvent: theEvent]; [super keyDown: theEvent]; } @end </code></pre> <p>Sorry for the long winding code, but the error is driving me nuts. Thanks for any help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload