0

Is there a way to add the mouse event to an Action in Interface Builder?

Currently in my ViewController.h I have this:

- (IBAction)myStepperAction:(id)sender;

In my ViewController.m I have this:

- (IBAction)myStepperAction:(id)sender { }

In my event handler I have the following: (I'm using C# but I can read some Swift)

    partial void myStepperAction(Foundation.NSObject sender) {
        var stepper = sender as NSStepper;
    }

If I add a second argument I get an error:

    partial void myStepperAction(Foundation.NSObject sender, Event event) {
        var stepper = sender as NSStepper;
        var value = event.CTRLKey==true ? stepper.DoubleValue + 10 : stepper.DoubleValue;
    }

CS0759: No defining declaration found for implementing declaration of partial method 'ViewController.myStepperAction(NSObject, EventArgs)'

Is there a way to pass the event to a partial method? Or is there a way to get an application mouse event?

What I want to do is when the stepper is clicked, check if the CTRL or SHIFT key is also pressed to make it increment by 10 instead of 1.

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
  • If I understand correctly do you want to increase the value by 1 when directly click on the stepper, and increase the value by 10 when click on the stepper with press CTRL or SHIFT key ? – ColeX May 26 '21 at 07:53
  • @ColeX-MSFT that is correct – 1.21 gigawatts May 26 '21 at 07:55

1 Answers1

1

You can use CGEventSourceKeyState to detect whether the specific key is pressed currently or not .

And you can find the exact number for control in this link .

It should be 0x3B .

Check the code used in Xamarin below

partial void myStepperAction(Foundation.NSObject sender) {
    var stepper = sender as NSStepper;

    bool isCtrlPressed = CGEventSource.GetKeyState(0,0x3b);
    var value = isCtrlPressed ? stepper.DoubleValue + 10 : stepper.DoubleValue;
       
}
ColeX
  • 14,062
  • 5
  • 43
  • 240
  • This works great. Would you happen to know if the user presses the up or down button? – 1.21 gigawatts May 26 '21 at 22:57
  • Just find the code in the link i provided , the code for up and down button is `0x7E` and `0x7D` . – ColeX May 27 '21 at 01:24
  • I didn't word that fully. I meant the buttons on the stepper. I've created a separate question for that. https://stackoverflow.com/questions/67713746/way-to-check-if-up-or-down-button-is-pressed-with-nsstepper – 1.21 gigawatts May 27 '21 at 01:26