c# – How to intercept keys not defined in VSStd2K, such as Shift+Tab?


I attempted to intercept key combinations using IOleCommandTarget for commands within VSConstants.VSStd2KCmdID.

I tried BACKTAB, but it doesn’t work, even though TAB can be intercepted. I suspect that Shift+Tab hasn’t been defined. Furthermore, the documentation regarding BACKTAB does not explicitly mention the Shift+Tab combination.

For example:

using System;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Text.Editor;
using Constants = Microsoft.VisualStudio.OLE.Interop.Constants;

public sealed class KeySeleteCompletionHandler : IOleCommandTarget
{
    ...
    public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
    {
        Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
        if (pguidCmdGroup == VSConstants.VSStd2K)
        {
            IAsyncCompletionSession session = _completionBroker.GetSession(_textView);
        
            if (session != null)
            {
                if (prgCmds[0].cmdID == (uint)VSConstants.VSStd2KCmdID.TAB ||
                    prgCmds[0].cmdID == (uint)VSConstants.VSStd2KCmdID.BACKTAB) // The tab is VSStd2KCmdID.TAB but the BACKTAB is Shift+Tab?
                {
                    prgCmds[0].cmdf = (uint)(Constants.MSOCMDF_SUPPORTED | Constants.MSOCMDF_ENABLED);
                    return VSConstants.S_OK; 
                }
            }
        }
        
        if (NextHandler != null)
        {
            return NextHandler.QueryStatus(ref pguidCmdGroup, cCmds, prgCmds, pCmdText);
        }
        
        return (int)Constants.OLECMDERR_E_UNKNOWNGROUP;
    }
    ...
}

Because of this, I am looking for a general approach to intercept key combinations without relying on key combinations predefined by VSStd2KCmdID.

Leave a Reply

Your email address will not be published. Required fields are marked *