0

Currently I have an IBAction for a NSButton, and the NSButton also has a key equivalent. I would like to know if there is a way inside my IBAction function "strike"

- (IBAction)strike:(id)sender

that can tell me whether this action is triggered by a mouse click on the button or a press of the key equivalent of the button?

wc373
  • 45
  • 1
  • 7
  • NSEvent. You want NSEvent. You can check the event to see how it was called. But IBAction passes id for sender by default. It is good for checking if an action always triggered by a button or menu or other object. What are you trying to accomplish? – uchuugaka Aug 07 '13 at 05:11
  • There is no way of getting NSEvent from sender? I'm basically writing a little virtual instrument program, and I want the key (NSButton) to light up differently depending on whether it's triggered by a click or a key press. – wc373 Aug 08 '13 at 04:47
  • I guess sender and NSEvent are totally different things...Maybe I'll try to think of another way to accomplish the same effect. – wc373 Aug 08 '13 at 04:50
  • 1
    You could create a very thin subclass of the button or control. Only add an event property and override the keyDown and mouseDown to capture that event and call super to behave as expected. Then you can ask sender how the action was invoked. – uchuugaka Aug 08 '13 at 05:31
  • A category might work also and enable you to use the normal class – uchuugaka Aug 08 '13 at 05:32
  • This is an overdue reply, but I was able to solve the problem with a subclass that stores a NSEventType, as you suggested. Thank you so much! – wc373 Oct 13 '13 at 02:58

1 Answers1

1

get the current event using -currentEvent and using if loop check for mouse click count

if([theEvent clickCount]>=1)
{
mouse clicked;
}
else
{
button pressed;
}
Muruganandham K
  • 5,271
  • 5
  • 34
  • 62