3

I'm trying to create an add-in for ArcMap in Visual Studio that adds points on mouse clicks and then executes some geoprocessing on those points when the enter key is pressed.

So far sifting through the ArcObjects help has yielded a few things that I've tried unsuccessfully to put together:

I'm wondering if it's even possible to have more than one of these "events" activated by a tool or if I'm barking up the wrong tree and should be attempting a different method?

Here is a skeleton of some code I have so far:

namespace MultiEventTest
{
    public class Tool1 : ESRI.ArcGIS.Desktop.AddIns.Tool
    {
        public Tool1()
        {
        }

        protected override void OnUpdate()
        {
            Enabled = ArcMap.Application != null;
        }

        protected override void OnMouseDown(MouseEventArgs arg)
        {
        //code to add points to feature class
        }

        protected override void OnKeyDown(KeyEventArgs arg)
        {
            if (arg.ModifierKeys == (System.Windows.Forms.Keys.Enter))
            {
                //code to execute geoprocessing
            }
        }

        }
    }

2 Answers 2

1

The event may not be wired by default, I don't think this is the case here. I think you have the wrong property, it should be arg.KeyCode == not arg.ModifierKeys ==, modifiers are shift, alt, Ctrl. I think the OnKeyDown is working but not entering the block because the value is None.

if (arg.KeyCode == System.Windows.Forms.Keys.Enter)

Then check your modifiers for Shift, Ctrl.. It is quite confusing as both return Keys enumeration however the actual (a-z, 1-9) key pressed is returned by KeyCode and if any special keys are down at the same time that is returned by the ModifierKeys, which is kind of redundant as the arg contains alt, control and shift properties as boolean.

Event interfaces can be very difficult to debug as the events come (in some instances) very quickly (like MoseMove) and the code can be fired while still responding to a previous event... beware of that or it will get you!

As for can you have more than one event per tool: absolutely! in some cases I even wire additional events like OnStartEditing, OnStopEditing, ViewRefreshed...

2
  • Wow, thanks, this definitely works. I was basing my implementation off of ESRI's help file which definitely uses the modifier setup. Thanks so much. Commented Jun 27, 2015 at 17:44
  • If this answer has solved your problem please check the accept mark on the upper left to indicate to future users that the answer has helped you... I am glad that this has helped; usually the help file is of the highest quality but occasionally there is a small error, please don't let this instance tarnish your enthusiasm/trust for the Esri help docs this is, although not an isolated instance, certainly a rare event. Commented Jun 28, 2015 at 1:25
1

I haven't tested this but I don't there would be any problem. I suggest to simply test the code with two MessageBox:

public class Tool1 : ESRI.ArcGIS.Desktop.AddIns.Tool
{
    public Tool1()
    {
    }

    protected override void OnUpdate()
    {
        Enabled = ArcMap.Application != null;
    }

    protected override void OnMouseDown(MouseEventArgs arg)
    {
        //code to add points to feature class
        MessageBox.Show("OnMouseDown");
    }

    protected override void OnKeyDown(KeyEventArgs arg)
    {
        if (arg.ModifierKeys == (System.Windows.Forms.Keys.Enter))
        {
            //code to execute geoprocessing
            MessageBox.Show("OnEnterKey");
        }
    }

    }
}
1
  • This is good thinking. When I try it however, the OnMouseDown pops up but the OnKeyDown doesn't. Still struggling with this. Maybe I'll try one of the different events and see if I get a different result. Commented Jun 27, 2015 at 2:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.