1

I need to simulate a button press on my Cocoa button programatically and I am trying to do this on cocotron which unfortunately does not have the NSEvent method: mouseEventWithType:location:modifierFlags:timestamp:windowNumber:context:eventNumber:clickCount:pressure: implemented. Is there anyway to programatically simulate a button press without having to create an event?

Rupesh Yadav
  • 12,096
  • 4
  • 53
  • 70
Mike2012
  • 7,629
  • 15
  • 84
  • 135

1 Answers1

6

Don't forget to look in superclasses when you're looking for something. All NSControls, including all NSButtons, respond to a performClick: message.

That said, is it really appropriate for you to simulate a button press? If you just want something done, it's generally better to directly tell the controller to do it.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • I have a menu item and a button that do the same thing. The click on the button also manages the alternate text and state of the button. What do you think, is this a good reason to use `performClick:`? – Michele Percich Sep 08 '14 at 17:27
  • 1
    @MichelePercich: No. The menu item and button should simply have the same action and (if necessary) target, and the button should be kept updated either by Bindings (binding the title and state to relevant properties of a controller) or by your own code (after changing your state, update the button to match). – Peter Hosey Sep 11 '14 at 03:50