Cocoa:接受并响应击键
大家好,我是一个新手,我预计这将是一个很容易回答的问题。为了了解一些有关事件处理和绘图的知识,我尝试编写一个程序来绘制一个黑色矩形,每次用户按“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
IIRC,要接收击键,您的视图需要成为第一响应者。如果您添加类似以下方法的内容,它应该可以工作:(
当然,如果合适的话,您也可以在其中执行其他操作。)
更新: 您还需要将视图标记为需要重新绘制。添加:
到您的事件处理程序。在开头添加日志消息可能是一个好主意,以便您可以查看该方法是否被调用:
IIRC, to receive keystrokes your view needs to become first responder. It should work if you add something like these methods:
(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:
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: