检测 Visual Studio 编辑器中的活动
我想知道是否有一个以编程方式连接到 Visual Studio 编辑器的钩子,以便我可以确定是否有人正在编辑器中键入内容。
谢谢!
I would like to know if there is a programmatically hook into the visual studio editor, so that I can determine whether or not someone is typing in the editor.
Thanks!
在 Visual Studio 中可以通过多种方法来执行此操作。这里有一些不同的可用钩子。
IOleCommandTarget
:Visual Studio 中的击键最终将自身呈现为命令,并通过此链进行路由KeyProcessor
:对于直接 WPF 键盘输入,您可以创建 MEFIKeyProcessorProvider
code> 组件并让创建的KeyProcessor
处理输入ITextBuffer::Changed
:直接侦听底层缓冲区中的更改以解释输入。其中每一个都相当复杂,在一个 SO 问题中为它们添加完整的样本是不合理的。但只要在谷歌中输入他们的名字就可以让你找到正确的方向。
请注意,Visual Studio 中的键盘输入是一个非常复杂的过程。它必须处理 Windows 消息、旧式 Visual Studio 命令、WPF、MEF 挂钩、翻译加速器等……这非常混乱,并且试图在任何一个瓶颈点处理所有输入都充满了问题。我在开发插件时已尽力记录 Visual Studio 键盘输入的当前状态,您可以在这里找到它
但这还不是一个完整的理解。
There are a number of ways to do this in Visual Studio. Here are a few different hooks available.
IOleCommandTarget
: Key strokes in Visual Studio will eventually present themselves as commands and be routed through this chainKeyProcessor
: For straight WPF keyboard input you can creata MEFIKeyProcessorProvider
component and let the createdKeyProcessor
handle the inputITextBuffer::Changed
: Listen directly to changes in the underlying buffer to interpret input.Each of these is fairly complex and adding a full sample for them in an SO question is just not reasonable. But just typing their names into google should get you pointed in the correct direction.
Do note though that keyboard input in Visual Studio is a very complex process. It has to deal with windows messages, old style Visual Studio commands, WPF, MEF hooks, translating accelorators, etc ... It's very messy and trying to tackle all input at any one choke point is fraught with problems. I've done my best to document the current state of Visual Studio keyboard input while working on a plugin and you can find it here
It's hardly a complete understanding though.