Hey everyone, I'm a newbie and I have what I anticipate will be a pretty easy
question to answer. In order to learn a bit about event handling and drawing,
I'm attempting to write a program that draws a black rectangle that increases
in length every time the user hits the 'c' key. So far it just draws a black
rectangle on a blue background without responding to keystrokes.
Here is what I have so far:
Input.h
#import <Cocoa/Cocoa.h>
@interface Input : NSView {
int length;
}
-(void)keyDown:(NSEvent *)theEvent;
@end
Input.m
#import "Input.h"
@implementation Input
-(id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
length = 10;
if (self) {
//Initialization code here.
}
return self;
}
-(void)drawRect:(NSRect)dirtyRect {
//set variables
NSRect r1;
NSBezierPath *bp;
//set background color
[[NSColor blueColor] set];
NSRectFill(dirtyRect);
//set color to black & draw r1
[[NSColor blackColor] set];
r1 = NSMakeRect(1, 1, length, 10);
bp = [NSBezierPath bezierPathWithRect:r1];
[bp fill];
}
-(void)keyDown:(NSEvent *)theEvent {
NSString *key = [theEvent characters];
if ( [key isEqualToString:@"c"] ) {
length += 10;
}
}
@end
I copied the keyDown method from Cocoa in a Nutshell, by the way. Needless to
say, I don't really understand it. Basically, I would love it if somebody
could help me to get this program to work, because as of yet I have not gotten
anything to respond to keystrokes. I believe that I need to make Input the
First Responder, but I'm not sure about that and I don't know how to
anyway...this seems as if this should be such a basic program to create, but
it's giving me endless frustration. Please help!
Also the only thing I've done in IB is add a Custom View and change its class
identity to Input. _______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]