Issues after I add a command filter to textview

耗尽温柔 提交于 2019-12-11 00:54:42

问题


I want to intercept key events in vs. I searched many articles for help, and this article inspired me. What I have done is:

  1. create a new class and implement "IVsTextManagerEvents" interface to register every textview.

    public void OnRegisterView(IVsTextView pView)
    {
        CommandFilter filter = new CommandFilter();
        IOleCommandTarget nextCommandTarget;
        pView.AddCommandFilter(filter, out nextCommandTarget);
        filter.NextCommandTarget = nextCommandTarget;
    }
    
  2. add new class "CommandFilter" which implement IOleCommandTarget , in which we can intercept olecommand from vs

    public class CommandFilter : IOleCommandTarget
    {    
    
        public IOleCommandTarget NextCommandTarget
        {
            get; 
            set;
        }
    
        public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
        {
            NextCommandTarget.QueryStatus(ref pguidCmdGroup, cCmds, prgCmds, pCmdText);
            return VSConstants.S_OK;
        }
    
        public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (pguidCmdGroup == typeof(VSConstants.VSStd2KCmdID).GUID)
            {
                switch (nCmdID)
                {
                    case (uint)VSConstants.VSStd2KCmdID.RETURN:
                        MessageBox.Show("enter");
                        break;
                }
            }
    
            NextCommandTarget.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
    
            return VSConstants.S_OK;
        }
    }
    
  3. we need to advise IVsTextManagerEvents in Initialize

    protected override void Initialize()
    {
        base.Initialize();
    
        IConnectionPointContainer textManager = (IConnectionPointContainer)GetService(typeof(SVsTextManager));
        Guid interfaceGuid = typeof(IVsTextManagerEvents).GUID;
        textManager.FindConnectionPoint(ref interfaceGuid, out tmConnectionPoint);
        tmConnectionPoint.Advise(new TextManagerEventSink(), out tmConnectionCookie);
    }
    

after above prepare, we can now intercept key events. you can see a message box after you stroke key "enter".

My question is, after I have done above

  1. I can't save the document, that means when I stroked ctrl+S, nothing happened.
  2. You can see obvious delay, when I key in words. It seems my package take a long time to handle something, but as you can see above, I didn't at all.

回答1:


It seems I have found the answer!

Not:

NextCommandTarget.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);

return VSConstants.S_OK;

But:

return NextCommandTarget.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);


来源:https://stackoverflow.com/questions/8544203/issues-after-i-add-a-command-filter-to-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!