在可可中使用箭头键?
我对此做了一些研究,我发现 这个 问题。我实现了那里使用的代码,但什么也没发生。这是我正在使用的确切代码:
.h 文件
#import <Cocoa/Cocoa.h>
@interface Test : NSView {
}
-(void)keyUp:(NSEvent*)event;
-(void)keyDown:(NSEvent*)event;
@end
.m 文件
#import "Test.h"
@implementation Test
- (void)keyDown:(NSEvent*)event {
NSLog(@"A key has been pressed");
switch( [event keyCode] ) {
case 126: // up arrow
case 125: // down arrow
case 124: // right arrow
case 123: // left arrow
NSLog(@"Arrow key pressed!");
break;
default:
NSLog(@"Key pressed: %@", event);
break;
}
}
@end
有什么问题吗?我需要在界面中添加一些内容吗?
编辑:嗯,确实发生了一些事情。我的电脑向我发出嘟嘟声。而已。
I was doing a little research into this, and I found
this
question. I implemented the code used there, but nothing happened. Here is the exact code I am using:
.h file
#import <Cocoa/Cocoa.h>
@interface Test : NSView {
}
-(void)keyUp:(NSEvent*)event;
-(void)keyDown:(NSEvent*)event;
@end
.m file
#import "Test.h"
@implementation Test
- (void)keyDown:(NSEvent*)event {
NSLog(@"A key has been pressed");
switch( [event keyCode] ) {
case 126: // up arrow
case 125: // down arrow
case 124: // right arrow
case 123: // left arrow
NSLog(@"Arrow key pressed!");
break;
default:
NSLog(@"Key pressed: %@", event);
break;
}
}
@end
What is wrong? Is there something that I have to add to the interface?
EDIT: Well, something actually did happen. I my computer beeped at me. Nothing more.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实现acceptsFirstResponder:
另外,请确保您的视图是第一响应者(例如,通过在视图内单击)。
Implement
acceptsFirstResponder
:Also, make sure that your view is first responder (e.g. by clicking inside the view).