Cocoa:接受并响应击键

发布于 2024-08-18 13:16:03 字数 1316 浏览 2 评论 0原文

大家好,我是一个新手,我预计这将是一个很容易回答的问题。为了了解一些有关事件处理和绘图的知识,我尝试编写一个程序来绘制一个黑色矩形,每次用户按“c”键时该矩形的长度就会增加。到目前为止,它只是在蓝色背景上绘制一个黑色矩形,而不响应击键。这是我到目前为止所拥有的:

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

顺便说一句,我从 Cocoa in a Nutshell 复制了 keyDown 方法。不用说,我真的不太明白。我是否必须在 IB 中建立连接才能让程序识别击键?基本上,如果有人可以帮助我让这个程序运行,我会很高兴,因为到目前为止我还没有得到任何响应击键的信息。

这是可可简而言之

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满地尘埃落定 2024-08-25 13:16:04

IIRC,要接收击键,您的视图需要成为第一响应者。如果您添加类似以下方法的内容,它应该可以工作:(

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

当然,如果合适的话,您也可以在其中执行其他操作。)

更新: 您还需要将视图标记为需要重新绘制。添加:

[self setNeedsDisplay:YES];

到您的事件处理程序。在开头添加日志消息可能是一个好主意,以便您可以查看该方法是否被调用:

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

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]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文