OnKeyDown

Developer
Dec 15, 2010 at 1:53 PM

In our application we are using MapFunctions.  We have added functionality that makes use of the Shift key.  But we noticed some serious performance issues and bad behaviors when the Shift key was pressed.  We think we have identified what in Dot Spatial we need to fix, but wanted to run this past the group to make sure this is all good for everyone.

In Map.cs in the ParentFormKeyUp/Down handlers.  The code looks like this:

        void ParentFormKeyDown(object sender, KeyEventArgs e)
        {
            foreach (IMapFunction tool in MapFunctions)
            {
                tool.DoKeyDown(e);
            }
        }

Shouldn't we only call tool.DoKeyDown if the tool is enabled?  I will make that change if noone disagrees.

The second thing we noticed was in MapFunctionSelect.OnKeyDown which is shown below.  Notice that it always calls Map.MapFrame.Invalidate() which unlike Map.Invalidate() actually causes a complete redrawing of the map which can be costly.  My thinking is that Invalidate should only be called if the features are actually deleted.  Once we fix the first issue above, this one is not really a big deal as far as my application, but I thought I'd just mention it for the greater good.

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                bool selectable = false;
                foreach (ILayer layer in Map.GetLayers())
                {
                    IFeatureLayer fl = layer as IFeatureLayer;
                    if (fl == null) continue;

                    if (fl.IsWithinLegendSelection())
                    {
                        selectable = true;
                        fl.RemoveSelectedFeatures();
                        break;
                    }
                }
                if (selectable == false)
                {
                    MessageBox.Show(MessageStrings.MapFunctionSelect_OnKeyDown_No_Deletable_Layers);
                }
            }
            base.OnKeyDown(e);
            if (Map != null) Map.MapFrame.Invalidate();
        }

 

Developer
Dec 15, 2010 at 3:07 PM

Definitely this sounds like a great fix.  I didn't catch it before since I don't really use the keyboard for anything else, but this sounds exactly like a fix we need.

Ted