It sounds like you're looking for a good example / reference on how to do keyboard handling to drive text insertion into the Visual Studio buffer.
Unfortunately this is not a question that has a straight forward answer. Keyboard handling in Visual Studio is complex at best and hair pulling frustrating at times. I've tried to keep this answer as simple as possible but unfortunately the topic doesn't lend itself to simplicity
To start there are at least 5 different mechanisms by which Visual Studio routes keyboard input into commands.
Which of these you need to hook into depends on a couple of factors
- What versions of Visual Studio you want the addin to function in?
- What type of add-in will you be building: an actual Addin, Vs Package or VSIX
- How deeply you care about winning the battle for keyboard input
Which of these you need to hook into depends a lot on how you want your addin to function and in what versions of Visual Studio you want it to work. Though there are 2 basic routes you can take though
The first is develop a Visual Studio Package and register a DTE.Command
for every hotkey in your extension. You'd then need to add on IOleCommandTarget
into the IVsTextView
s filter chain to process your commands. This approach will allow you to process your hot keys in the majority of scenarios.
The second is to develop a VSIX, hook into the IOleCommandTarget
chain of the IVsTextView
and intercept real Visual Studio commands which map to your hot keys.
Both of these approaches have their ups and downs but my answer is already a bit too long as it is. If you can give me some more details I can try and give you a more concise and helpful answer.
As for examples. VsVim does pretty extensive keyboard handling for Visual Studio and has an example for any of the above methods I mentioned.
Let me know which one interests you and I can point you to the right place in the source code.
Additionally while working myself on figuring out the tangle of keyboard input I tried to keep a record of what all I'd learned. It's not fully up to date but this document describes the more basic aspects of how input gets routed
EDIT
Jon indicated in comments the VSIX + IOleCommandTarget
route
This is IMHO the simplest approach. The one thing to be aware of though is how command bindings affects the data which gets passed down the IOleCommandTarget
chain. Visual Studio command binding happens before the data is passed to the IOleCommandTarget
chain. So if a given key input is bound to a command it will be passed down IOleCommandTarget
as the command and not the keyboard input.
For example CTRL-Z
is commonly mapped to the Visual Studio Undo
command. If the user hits CTRL-Z
then IOleCommandTarget
will see the Undo command and not the keyboard input CTRL-Z
.
Here are a few samples you may be interested in looking at
- VsCommandTarget: Sample implementation of
IOleCommandTarget
- OleCommandUtil: Used to convert the data passed into
IOleCommandTarget
into actual commands and keyboard input
- HostFactory: Details how actually hookup an
IOleCommandTarget
into an IVsTextView
from a VSIX extension.