Swift NSMenuItem detect shift click

时间:2016-03-04 17:50:39

标签: swift macos nsmenu nsmenuitem

I'm currently adding an NSMenuItem to my NSMenu and I'm able to successfully detect and handle the click event. However, I want to add a second option and detect if the user has clicked the menuItem while holding SHIFT

let menuItem = myMenu?.submenu?.addItemWithTitle("Click me", action: "itemClicked:", keyEquivalent: "")

Is there any way to do this? I looked at keyEquivalent but I'm not seeing ANYTHING to how to get this to work.

Thanks

1 个答案:

答案 0 :(得分:3)

感谢@LeoDabus提供了一些帮助,但我能够在我的NSMenuItem的事件处理程序中使用以下代码解决此问题:

if let event = NSApp.currentEvent {
    if event.modifierFlags.contains(.ControlKeyMask) {
        // success!
    }
}

快乐编码