Cocoa: Accepting and responding to keystrokes

♀尐吖头ヾ 提交于 2019-12-23 04:53:08

问题


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. Do I have to make connections in IB in order to get the program to recognize keystrokes? 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.

And here's Cocoa in a Nutshell


回答1:


IIRC, to receive keystrokes your view needs to become first responder. It should work if you add something like these methods:

- (BOOL) acceptsFirstResponder
{
    return YES;
}
- (BOOL) resignFirstResponder
{
    return YES;
}
- (BOOL) becomeFirstResponder
{
    return YES;
}

(You can do other stuff in them too, of course, if appropriate.)

Update: You also need to mark your view as needing to be redrawn. Add:

[self setNeedsDisplay:YES];

To your event handler. And it's probably a good idea to add a log message at the beginning as well, so that you can see whether the method is getting called:

NSLog(@"keyDown [%@]", [theEvent characters]);


来源:https://stackoverflow.com/questions/2105468/cocoa-accepting-and-responding-to-keystrokes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!