Swift NSMenuItem detect shift click - swift

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

Thanks to #LeoDabus for some help but I was able to solve this in the event handler for my NSMenuItem with the following code:
if let event = NSApp.currentEvent {
if event.modifierFlags.contains(.ControlKeyMask) {
// success!
}
}
happy coding

Related

How do I keep NSMenuItem selected in a NSPopover with NSMenu?

I have a NSPopUpButton with a built up NSMenu.
However, when an NSMenuItem is selected, it keeps going back to the first item.
In this example, I'd expect "Maged" to be selected.
Any ideas?
Related question/answer: https://stackoverflow.com/a/53387962/157656
Duplicate:
It's been suggested that this is a duplicate of the question
How do I change/modify the displayed title of an NSPopUpButton
"I would like an NSPopUpButton to display a different title than the title of the menu item that is selected."
However, my question was about getting the NSPopUpButton to show the selected item.
In the end, I changed how I did it.
I am using a NSButton to show the menu and NSTextField to display the results.
If anyone is interested in the details, here they are.
Build the menu up and use .representedObject to store whatever you need to access at the other end. I used a struct with the name and code the in it.
You need to assign the NSMenu to the NSButton.menu
Then have a click, something like this.
#IBAction func changeVoiceClicked(_ sender: NSButton)
{
if let event = NSApplication.shared.currentEvent {
NSMenu.popUpContextMenu(sender.menu!, with: event, for: sender)
}
}
Your NSMenuItem should have an action on it, which using a selector points to a function.
Something like this:
#objc func voiceChanged(sender: NSMenuItem)
{
// cope will nil
var voice : VoiceDetail = VoiceDetail();
if (sender.representedObject != nil) {
voice = sender.representedObject as! VoiceDetail;
}
// Do what you need to on menu select.
// update text field.
}

Weird issue with NSMenu

I am showing NSMenu with below method. Mouse over events and actions on MenuItems are working fine.
if let event = NSApplication.shared.currentEvent
NSMenu.popUpContextMenu(menu, with: event, for: self.view)
}
Now, to have this menu at different position I changed this method to below and all the events like mouse over and selections are not working.
menu.popUp(positioning: item1, at: position, in: self.view)
Please suggest me solution.
Thanks in advance.

Disable/enable NSMenu item

I've created a menu bar app, a NSMenu object using the Interface Builder (following this tutorial). The menu has two items:
Start Commando
Stop Commando
How can I disable/enable the menu items when they're clicked? I've set disabled "Auto Enables Items" and I can manually enable/disable the items in the Attributes inspector, but how can I achieve the same thing when their functions are called?
When "Start Commando" is clicked I want the item to disable and "Stop Commando" to enable. And the other way around when "Stop Commando" is clicked.
Swift provides with setEnabled property that can be used on NSMenuItem you are trying to enable or disable.
You can do the following :
#IBOutlet weak var startMenuItem: NSMenuItem!
startMenuItem.isEnabled = false or true
You can try below code :
let menu = NSMenu();
menu.autoenablesItems = false
As others say, there is a isEnabled property for NSMenuItems. One also needs to uncheck Auto Enables Items for that menu or sub-menu in the Attributes Inspector in Xcode, or through code, to allow the setting to take effect.
To get it to change on selection, in the IBAction called for the menu item, likely in your NSWindowController, do something like this:
#IBAction private func myMenuAction(sender: NSMenuItem) {
sender.isEnabled = false
}
You will not be able to then select the menu item afterwards. I assume you re-enable it else where as so:
if let appDelegate = NSApplication.shared.delegate as? AppDelegate {
appDelegate.myMenuItem.isEnabled = true
}
Code untested.
Declare a BOOL value for instance
BOOL isActive
if(isActive)
{
//show menu
}
else
{
//hide your menu
}
also make BOOL true when your view dismiss

How can I prevent user from clicking the button twice? Parse Xcode Swift

Hi I am making a app that basically updates users on what events are going on in the city, I have it hooked up to parse, so i am able to post new events from parse and if a user clicks "go to this event" i receive the user's info in parse, the only problem I am running into is I don't want the user to be able to click the "go to this event" twice. can someone please help me with preventing user from clicking the button twice. when the user click the button first it takes him to "thank you" view controller, and if the user clicks second time i want to basically say "you are already going to this event"
this is what I am doing to get user's info in parse:
/////// GO TO THE EVENT BUTTON
#IBAction func Going(sender: AnyObject) {
let sweet:PFObject = PFObject(className: "Going")
sweet["userprofile"] = PFUser.currentUser()
sweet["first_name"] = PFUser.currentUser()?["first_name"] as? String
sweet["last_name"] = PFUser.currentUser()?["last_name"] as? String
sweet["email"] = PFUser.currentUser()?["email"] as? String
sweet["club"] = club.text
sweet["date"] = date.text
sweet["eventdetails"] = eventdetails.text
I think that it will have to do something with the userprofileID, so that if the same ID click the Button with EVENTDETAILS"------" (because event details would define the events the most i think and would make it unique) to show the view controller "You are already going to this event"
I am using swift 2 Xcode 7
I am new to programming, I hope someone can help
Create an outlet for the button besides the IBAction that you already created
#IBOutlet var login: UIButton! // rename it to whatever you want
#IBAction func Going(sender: AnyObject) {
// For disabling button
self.login.enabled = false // disable it on click of the button.Change it to true for enabling
// Now write your other code below

Adding long press gesture recognizer to UITextView

I'm trying to let the user move a text view around the screen as he presses down on it. The issue I'm getting is my gesture recognizer isn't getting called and instead the text is being selected when I press down on it. How can I disable this? I can't just disable user interaction because a tap should still let the user edit the text. My code is below
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
text.addGestureRecognizer(longPressRecognizer)
func longPressed(sender : UITextView) {
println("long press")
}
Try adding this tiny piece of code. Sometimes it makes magic happen ;)
text.userInteractionEnabled = true