检测可可上的鼠标右键单击

2024-05-02

我正在尝试在我的原型中管理鼠标事件精灵套件 game.

我从问题中使用了以下方法SO_q https://stackoverflow.com/questions/17473176/how-to-detect-right-and-left-click-in-cocoa

- (void) mouseDown: (NSEvent*) theEvent
{
    NSLog(@"Click!");
}

- (void) rightMouseDown:(NSEvent*) theEvent
{
    NSLog(@"DERECHA PULSADA!");
}

但检测右键单击的方法对我不起作用。我想检测鼠标右键的点击和放下。 如何检测鼠标单击何时被放下?

UPDATE:

我尝试使用以下方法,从Cocoa事件处理文档 https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/EventOverview/HandlingMouseEvents/HandlingMouseEvents.html#//apple_ref/doc/uid/10000060i-CH6-SW1.

- (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:仅处理左键单击事件。

阅读完后Cocoa事件处理文档 https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/EventOverview/HandlingMouseEvents/HandlingMouseEvents.html#//apple_ref/doc/uid/10000060i-CH6-SW1我尝试覆盖以下方法:

-(void)rightMouseDown:(NSEvent *)theEvent
{
    NSLog(@"DERECHO PUSSSHHH!");
}

- (void)rightMouseUp:(NSEvent *)theEvent
{
    NSLog(@"Botón Derecho Soltado!");
}

也没起作用。

UPDATE:

正如我之前所说,这些方法位于 Sprite-Kit 类中。这里是定义这些方法的类定义。

#import <SpriteKit/SpriteKit.h>

@interface OpcionesMenu : SKScene

@end

如果您查看 Apple 的文档NSView https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/classes/NSView_Class/Reference/NSView.html你会看到 NSView 的 rightMouseDown: 实现不会将鼠标右键按下事件发送到响应者链,而是通过调用直接处理该事件menuForEvent:.

SKView是NSView的子类,所以我通过向SKView添加一个类别并实现我自己的版本来解决这个问题rightMouseDown:。我的 rightMouseDown 版本:仅调用我的场景实现rightMouseDown:,这是我在场景中实际执行操作的地方。 SKView 有一个属性 scene,它保存对当前呈现场景的引用。因此,我的 rightMouseDown: 类别实现如下所示:

ObjC:

@implementation SKView (Right_Mouse)
-(void)rightMouseDown:(NSEvent *)theEvent {
     [self.scene rightMouseDown:theEvent];
}
@end

我在我的文章中详细阐述了这一点blog http://www.two-tangled-trees.com/1/post/2014/02/right-mouse-down-events-in-sprite-kit.html如果你有兴趣的话。

斯威夫特 3 + iOS10:

extension SKView {
  open override func rightMouseDown(with theEvent: NSEvent) {
    self.scene?.rightMouseDown(with: theEvent)
  }
}

Swift:

extension SKView {
    public override func rightMouseDown(theEvent: NSEvent) {  
        self.scene?.rightMouseDown(theEvent)
    } 
}

并将其添加到您的SKScene子类

override func rightMouseDown(theEvent: NSEvent) {
    let location = theEvent.locationInNode(self)
    ...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

检测可可上的鼠标右键单击 的相关文章

随机推荐