如何处理在Cocoa应用程序中的箭头键事件? [英] How to handle arrow key event in Cocoa app?
本文介绍了如何处理在Cocoa应用程序中的箭头键事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
(解决方案注释)
如何处理Cocoa应用程序中的箭头键事件?
How to handle arrow key event in Cocoa app?
推荐答案
查看此代码。我假设该类是 NSView
的子类。
See this code. I assumed the class is subclass of NSView
.
#pragma mark - NSResponder
- (void)keyDown:(NSEvent *)theEvent
{
NSString* const character = [theEvent charactersIgnoringModifiers];
unichar const code = [character characterAtIndex:0];
switch (code)
{
case NSUpArrowFunctionKey:
{
break;
}
case NSDownArrowFunctionKey:
{
break;
}
case NSLeftArrowFunctionKey:
{
[self navigateToPreviousImage];
break;
}
case NSRightArrowFunctionKey:
{
[self navigateToNextImage];
break;
}
}
}
视图应为第一响应者接收事件。也许这个代码将需要支持。
A view should be first responder to receive events. Maybe this code will be required to support that.
#pragma mark - NSResponder
- (BOOL)canBecomeKeyView
{
return YES;
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
要使用此方法,类应该是 NSResponder
。请参阅其他答案处理,而不对NSResponder进行子类化。
To use this method, the class should be subclass of NSResponder
. See the other answer handling without subclassing NSResponder.
这篇关于如何处理在Cocoa应用程序中的箭头键事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文