views:

110

answers:

1

I want to write my own global snippets tool for Windows XP and higher. This would be an always running in the background tool that would pop-up on a globally-defined hotkey, allow me to select a snippet with substitution arguments, and then paste the expanded snippet into the text input of whatever control I had been in when activated it, and finally, return me to that previous app/input box.

I know how to do most of the algorithmic aspects, but I do not know how to accomplish these windows-based features:

1 - Global Hotkey: how do I define a key-sequence in windows (from .net?) that will work, even when entering data in another apps textbox? (Usually this will be a browser window)

2 - Pasting Into Another App: I could use the paste-buffer and Ctrl-C, but I want to avoid the extra keystrokes.

3 - Return Control to Original Window: Seamlessly return back into my input stream: how do I do that? In fact, how does my tool even know where I was before it popped up?

The reasons that I want to write this myself is first to learn how (because there are other tools like this I would like to make) and secondly, I don't know of any snippets tools that have the argument substitution that I want.

So, the two (2)questions are A) What should be my general approach? and B) how best can I accomplish items 1 to 3 above?

+2  A: 

You'll need to use global system keyboard hooks to capture your hotkey. There is a CodeProject article showing how to do this from within .NET.

Once you've "trapped" your keystroke, you can use the Windows API to get the current windows handle. However, I'd try to avoid activating your application. You should be able to just paste your new text, and allow the application to handle it.

The disadvantage of using the Windows API is that it doesn't work in all cases, and the "broken" cases are getting more and more common. For example, WPF applications do not provide a HWND for each element within a window, so getting the current "control"s handle will just give you the window, not the appropriate element.


Edit: Another reference source is this article in MSDN Magazine. It shows how to do this via C# using P/Invoke.

Reed Copsey
The article is about writing an unmanaged VC++ dll to enable this in VB/C#. Unmnaged VC++ is a bit beyond me, i there anyway closer to home?
RBarryYoung
Here's another article: http://msdn.microsoft.com/en-us/magazine/cc188966.aspx This one is done entirely in C#, using P/Invoke. Unfortunately, you need to use P/Invoke and the Windows API for this, as there's no hooking API in the framework.
Reed Copsey
I encourage you to move the second reference into your answer, I definitely found it more helpful.
RBarryYoung
@RBarryYoung: Done.
Reed Copsey