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
  • 14,347
  • 30
  • 103
  • 209

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 - MSFT
  • 12,883
  • 4
  • 34
  • 229
  • 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 - MSFT 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