In visual studio experimental instance I need to remove keyboard shortcut for "Cut" and "Delete" in Solution Explorer

Mekala Meghana 20 Reputation points
2023-11-30T06:27:02.56+00:00
 I am trying to remove keyboard shortcut for CUT and Delete in experimental instance for Solution Explorer. The below code is working globally (For entire visual studio its restricting). I need to restrict it to Solution explorer, when we click on any specific file, Cut and Delete keyboard shortcuts should not work.

This is my code snippet that I have tried:

DTE dte = (EnvDTE.DTE)Package.GetGlobalService(typeof(EnvDTE.DTE));
Command command = dte.Commands.Item("Edit.Cut", -1);

if (command.Bindings != null)
{
   command.Bindings = new object[];
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,602 questions
Visual Studio Extensions
Visual Studio Extensions
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Extensions: A program or program module that adds functionality to or extends the effectiveness of a program.
191 questions
{count} votes

1 answer

Sort by: Most helpful
  1. gekka 8,061 Reputation points MVP
    2023-11-30T14:40:25.39+00:00

    Deleting item in SolutionExplore using keyboard or mouse, it shuld be need the SolutionExolore window is active.
    So, this can be achieved by canceling SolutionExolore only if it is active.

    using Microsoft.VisualStudio.Shell;
    using System;
    using System.Runtime.InteropServices;
    using System.Threading;
    using Task = System.Threading.Tasks.Task;
    
    
    [ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.NoSolution, PackageAutoLoadFlags.BackgroundLoad)]
    [ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.EmptySolution, PackageAutoLoadFlags.BackgroundLoad)]
    [ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.SolutionExists, PackageAutoLoadFlags.BackgroundLoad)]
    [ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.SolutionHasSingleProject, PackageAutoLoadFlags.BackgroundLoad)]
    [ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.SolutionHasMultipleProjects, PackageAutoLoadFlags.BackgroundLoad)]
    [PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
    [Guid(VSIXProject1Package.PackageGuidString)]
    public sealed class VSIXProject1Package : AsyncPackage
    {
        public const string PackageGuidString = "f41ec426-229f-4e09-8e88-1fa351c64561";
    
        #region Package Members
    
        protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
        {
            await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
    
            dte = (EnvDTE80.DTE2)await GetServiceAsync(typeof(EnvDTE.DTE));
            if (dte == null) return;
    
            cev = dte.Events.CommandEvents;
            cev.BeforeExecute += Cev_BeforeExecute;
    
            var cmdDelete = dte.Commands.Item("Edit.Delete");
            edit_delete = new GuidID(cmdDelete.Guid, cmdDelete.ID);
    
            var cmdCut = dte.Commands.Item("Edit.Cut");
            edit_cut = new GuidID(cmdCut.Guid, cmdCut.ID);
        }
    
        EnvDTE80.DTE2 dte;
        EnvDTE.CommandEvents cev;
    
        GuidID edit_delete;
        GuidID edit_cut;
    
        private void Cev_BeforeExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
    
            var cmd = new GuidID(Guid, ID);
            if (object.Equals(cmd, edit_delete) || object.Equals(cmd, edit_cut))
            {
                var sol = dte.ToolWindows.SolutionExplorer;
                var solutionWindow = dte.ToolWindows.SolutionExplorer.Parent;
                if (solutionWindow == dte.ActiveWindow)
                {
                    CancelDefault = true;
                }
            }
        }
    
        struct GuidID
        {
            public string Guid;
            public int ID;
    
            public GuidID(string guid, int id)
            {
                Guid = guid;
                ID = id;
            }
        }
        #endregion
    }
    
    0 comments No comments