检测右键单击可可 [英] Detect right mouse click on cocoa
问题描述
我正在尝试在我的 Sprite-kit 游戏原型中管理鼠标事件.
I'm trying to manage mouse events in my prototype of Sprite-kit game.
我从问题 SO_q 中使用了以下方法
I used the following methods from the question SO_q
- (void) mouseDown: (NSEvent*) theEvent
{
NSLog(@"Click!");
}
- (void) rightMouseDown:(NSEvent*) theEvent
{
NSLog(@"DERECHA PULSADA!");
}
但是对我来说,检测正确提示的方法不起作用.我想检测鼠标单击的单击和拖放. 我如何检测鼠标单击被何时放下?
But the method to detect the right clic doesn't work for me. I want to detect click and drop of right mouse click. How can i detect when the mouse click is dropped ?
更新:
I tried with the following mehod, picked up from the Cocoa Event Handling Doc.
- (void)mouseDown:(NSEvent *)theEvent
{
switch ([theEvent type])
{
case NSLeftMouseDown:
NSLog(@"ISQUIERDO down!");
break;
case NSLeftMouseUp:
NSLog(@"IZQDO soltado!");
break;
case RightMouseDown:
NSLog(@"DERECHO PUSSSHHH!");
break;
case NSRightMouseUp:
NSLog(@"Botón Derecho Soltado!");
break;
default:
/* Ignore any other kind of event. */
break;
}
return;
}
结果:仅处理了左键单击事件.
Result: Only event for the left click has been handled.
Following reading the Cocoa Event Handling Doc i tried overwriting the following methods:
-(void)rightMouseDown:(NSEvent *)theEvent
{
NSLog(@"DERECHO PUSSSHHH!");
}
- (void)rightMouseUp:(NSEvent *)theEvent
{
NSLog(@"Botón Derecho Soltado!");
}
也不行.
更新:
就像我之前说过的那样,这些方法在Sprite-Kit类中.这里是定义这些方法的类定义.
Like i said before, theses methods are in a Sprite-Kit class. Here the class definition where these methods are defined.
#import <SpriteKit/SpriteKit.h>
@interface OpcionesMenu : SKScene
@end
推荐答案
If you look at Apple's documentation for NSView you'll see that NSView's implementation of rightMouseDown: does not send right mouse down events up to the responder chain, but instead handles the event directly by calling menuForEvent:
.
SKView是NSView的子类,因此我通过向SKView添加类别并实现自己的rightMouseDown:
版本解决了此问题.我的rightMouseDown:版本仅调用rightMouseDown:
的场景实现,这是我在场景上实际执行操作的地方. SKView具有一个场景属性,其中包含对当前呈现的场景的引用.因此,我的rightMouseDown:类别实现如下:
SKView is a subclass of NSView, so I solved this problem by adding a category to SKView and implementing my own version of rightMouseDown:
. My version of rightMouseDown: merely calls my scenes implementation of rightMouseDown:
, which is where I actually perform actions on my scene. SKView has a property, scene, which holds a reference to the currently presented scene. Therefore, my category implementation of rightMouseDown: looks like this:
ObjC:
@implementation SKView (Right_Mouse)
-(void)rightMouseDown:(NSEvent *)theEvent {
[self.scene rightMouseDown:theEvent];
}
@end
我在博客(如果您有兴趣).
I elaborate on this a bit more on my blog if you're interested.
Swift 3 + iOS10:
extension SKView {
open override func rightMouseDown(with theEvent: NSEvent) {
self.scene?.rightMouseDown(with: theEvent)
}
}
快捷键:
extension SKView {
public override func rightMouseDown(theEvent: NSEvent) {
self.scene?.rightMouseDown(theEvent)
}
}
并将其添加到您的SKScene
子类
override func rightMouseDown(theEvent: NSEvent) {
let location = theEvent.locationInNode(self)
...
}
这篇关于检测右键单击可可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!