Visual Studio Add-in: Use Vista Search directly from Visual Studio

Published 04 March 07 12:41 PM | jannemattila 

I have used Windows Vista from last November and now I have noticed that I'm really heavy user of search. Before Vista I had a lot of problems finding my stuff because I just couldn't remember where I have saved my files. I used my own directory structures that would help me to find files easier. But still sometimes I just couldn't find them. Vista however makes this problem go away. I'll just press Windows key and start typing something relating to the item I'm looking for. And somehow my computer finds the correct results and really really fast too.

But few days ago I had and interesting idea. How could I bring that Vista search into my favorite tool: Visual Studio. So I decided to create Visual Studio Add-in that creates the query for me. I created new project with Add-in template:
Visual Studio: New project dialog 
Then I added following codes to the generated class:

 1  [DllImport("user32.dll", EntryPoint = "keybd_event")] 
 2  public static extern void KeyboardEvent(uint bVk,
 3     uint bScan, uint dwFlags, uint dwExtraInfo); 
 4  
 5  public const uint VK_LWIN = 0x5B; 
 6  public const uint KEYEVENTF_EXTENDEDKEY = 0x01;
 7  public const uint KEYEVENTF_KEYUP = 0x02; 
 8  
 9  public void Exec(String commandName, 
10                    vsCommandExecOption executeOption,
11                   ref object varIn, ref object varOut, ref bool handled)
12  {
13    handled = false;
14    if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
15    {
16      if (commandName == "VSExplorerAddin.Connect.VSExplorerAddin")
17      { 
18        handled = true;
19        EnvDTE.TextSelection selection = null; 
20        try
21        {
22          selection = _applicationObject.ActiveDocument.Selection 
23            as EnvDTE.TextSelection;
24          selection.GotoLine(selection.CurrentLine, false);
25          selection.EndOfLine(true);
26          String selectedText = selection.Text.Trim(); 
27          selection.Text = "";
28  
29          KeyboardEvent(VK_LWIN, 0, 0, 0); 
30          KeyboardEvent(VK_LWIN, 0, KEYEVENTF_KEYUP, 0); 
31          SendKeys.Send(selectedText + "{ENTER}");
32       

33        catch (Exception ex)
34        { 
35          if (selection != null)
36          {
37            selection.Text = ex.ToString(); 
38          } 
39          else 
40          { 
41            MessageBox.Show(ex.ToString()); 
42          } 
43        } 
44        return
45      }
46    }
47  }

Most of the stuff is put into the Exec-method, but added also KeyboardEvent (on lines 1 to 3) which is used to send the Windows key press. I needed that because I couldn't find way to send windows key press using the SendKeys. If that would be possible, it would just simplify this code a little bit more (so if you know better way then please let me know :-). But anyway the actual code is pretty simple. On lines 22 to 27 the current selection is copied into local variable and then the selection is cleared. And lines 29 and 30 just send the win key and finally on line 31 the previously stored selection is typed into the search box. And if something goes wrong (=exception is thrown) then we're setting the selection to have the error message or we'll just popup  a simple messagebox if selection is unavailable.

Do you want to see that stuff in action? Even if you don't I'm still going to show it to you :-) So first I'll just press F5 to start the project. Visual Studio actually launches new Visual Studio which can be used for testing my new addin. Then I just open any project of mine and wait it to be loaded. After that I can just type in my search word which is on line 16 (in this case word).

After I have typed in my search word I can launch my magical addin. In this case I'll use Alt-T (which opens Tools menu).
 
And when menu is open I'll just hit the Enter (since the addin is the first choice). After that my addin launches word as a search. Vista opens the first search result automatically because our addin sends Enter as last key press. As an end result we have Microsoft Office Word 2007 running:

This sample shows that you can easily add more functionality to Visual Studio. In this case I just added my favorite feature from Vista :) I hope you start creating you own addins rightaway! It's not that hard... believe me. Of course this could be done as a Macro as well, but I'll leave that stuff to another time.

Anyways... happy hacking!

J

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# jannemattila said on March 19, 2007 3:23 PM:

Hi everybody!

I got an email from Greg, who wanted to achieve this same functionality from the VS Macro. I did little test with following code:

Imports System

Imports EnvDTE

Imports EnvDTE80

Imports System.Diagnostics

Imports System.Runtime.InteropServices

Imports System.Windows.Forms

Public Module MyAddin

   <DllImport("user32.dll", EntryPoint:="keybd_event")> _

   Public Sub KeyboardEvent(ByVal bVk As System.UInt32, ByVal bScan As System.UInt32, ByVal dwFlags As System.UInt32, ByVal dwExtraInfo As System.UInt32)

   End Sub

   Public ReadOnly VK_LWIN As UInteger = 91

   Public ReadOnly KEYEVENTF_EXTENDEDKEY As UInteger = 1

   Public ReadOnly KEYEVENTF_KEYUP As UInteger = 2

   Sub Macro1()

       KeyboardEvent(VK_LWIN, 0, 0, 0)

       KeyboardEvent(VK_LWIN, 0, KEYEVENTF_KEYUP, 0)

       SendKeys.Send("word{ENTER}")

   End Sub

End Module

KeyboardEvent part works fine, but when you try to use SendKeys you got following error message:

"SendKeys cannot run inside this application because the application is not handling Windows messages. Either change the application to handle messages, or use SendKeys.SendWait method."

So you could make this stuff work from macro as well, but you can't use SendKeys.Send. No problem... just replace that line with this one:

       SendKeys.SendWait("word{ENTER}")

So here's the final code:

Imports System

Imports EnvDTE

Imports EnvDTE80

Imports System.Diagnostics

Imports System.Runtime.InteropServices

Imports System.Windows.Forms

Public Module MyAddin

   <DllImport("user32.dll", EntryPoint:="keybd_event")> _

   Public Sub KeyboardEvent(ByVal bVk As System.UInt32, ByVal bScan As System.UInt32, ByVal dwFlags As System.UInt32, ByVal dwExtraInfo As System.UInt32)

   End Sub

   Public ReadOnly VK_LWIN As UInteger = 91

   Public ReadOnly KEYEVENTF_EXTENDEDKEY As UInteger = 1

   Public ReadOnly KEYEVENTF_KEYUP As UInteger = 2

   Sub Macro1()

       KeyboardEvent(VK_LWIN, 0, 0, 0)

       KeyboardEvent(VK_LWIN, 0, KEYEVENTF_KEYUP, 0)

       SendKeys.SendWait("word{ENTER}")

   End Sub

End Module

Happy hacking!

J

# Colin said on April 14, 2007 7:08 AM:

Thank you!

[url=http://bguyjfvb.com/ddbx/trbw.html]My homepage[/url] | [url=http://atezmifj.com/hxxy/yegc.html]Cool site[/url]

# Debbie said on April 14, 2007 7:09 AM:

Well done!

<a href="http://bguyjfvb.com/ddbx/trbw.html">My homepage</a> | <a href="http://udufyljx.com/mzdy/fvbf.html">Please visit</a>

# Maggie said on April 14, 2007 7:10 AM:

Thank you!

http://bguyjfvb.com/ddbx/trbw.html | http://rjvbjcij.com/fvtm/cpey.html

# Bill said on May 13, 2007 7:04 AM:

MSN I NIIPET

<a href="http://msn.com">MSN</a>

# spellstomakemepsychic said on August 1, 2007 9:16 AM:

<a href="httpwwwdhcgpdiacnpage45html">abcseniormiddleeastcorrespondent</a> abcseniormiddleeastcorrespondent,<a href="httpwwwdhcgpdiacnpage64html">vb6getwindowsfolder</a> vb6getwindowsfolder,<a href="httpwwwdhcgpdiacnpage69html">jetairairwayslogobag</a> jetairairwayslogobag,<a href="httpwwwdhcgpdiacnpage69html">usairwaysdividendmilesvisasignaturecard</a> usairwaysdividendmilesvisasignaturecard,<a href="httpwwwdhcgpdiacnpage56html">egforumsgtdaviddrake</a> egforumsgtdaviddrake,<a href="httpwwwdhcgpdiacnpage63html">checkeredflagdie-cast</a> checkeredflagdie-cast,<a href="httpwwwdhcgpdiacnpage62html">harmlessdeception</a> harmlessdeception,<a href="httpwwwdhcgpdiacnpage66html">harveyfenton</a> harveyfenton,<a href="httpwwwdhcgpdiacnpage65html">photoshopelementsslideshow</a> photoshopelementsslideshow,<a href="httpwwwdhcgpdiacnpage56html">wwwegsparkcomtr</a> wwwegsparkcomtr,

# howfarcouldthewhistlesbebeheardofthetitanic said on August 1, 2007 2:56 PM:

<a href="httpwwwqrqplfsbcnpage49html">circleteachertelevisionseriesblackboard</a> circleteachertelevisionseriesblackboard,<a href="httpwwwqrqplfsbcnpage46html">howlingiiithemarsupialsimdb</a> howlingiiithemarsupialsimdb,<a href="httpwwwqrqplfsbcnpage41html">genresinlibraries</a> genresinlibraries,<a href="httpwwwqrqplfsbcnpage45html">clementefieldnewyork</a> clementefieldnewyork,<a href="httpwwwqrqplfsbcnpage50html">presariov2000batteries</a> presariov2000batteries,<a href="httpwwwqrqplfsbcnpage42html">michaeljordanlastshotphoto</a> michaeljordanlastshotphoto,<a href="httpwwwqrqplfsbcnpage35html">musiccomposedbydavidholmesinoceanstwelve</a> musiccomposedbydavidholmesinoceanstwelve,<a href="httpwwwqrqplfsbcnpage37html">johnsonjb-106</a> johnsonjb-106,<a href="httpwwwqrqplfsbcnpage55html">middleagebrewsters</a> middleagebrewsters,<a href="httpwwwqrqplfsbcnpage55html">summervacationrentalsandlongpondandbrewster</a> summervacationrentalsandlongpondandbrewster,

# Sasha said on October 5, 2007 11:24 AM:

Hi,

Came across the article. Had a quick question though? Is there a way by which I can create an Add-In to catch key presses in Visual Studio. Thanks!

Sasha

# jannemattila said on October 24, 2007 10:22 AM:

Hi Sasha!

Actually I don't believe that you can do that easily. Of course there are ways to start listening keyboard presses to achieve some kind of fancy features but you need to be careful not to make it spend too many CPU cycles on your custom feature.

What is that you're trying to achieve?

J

# Agusto Xaverius P.S said on May 23, 2008 1:34 AM:

This is the list of the Visual Studio Add-ins, most of which are open source or free. I hope this list

Leave a Comment

(required) 
(optional)
(required) 
Page view tracker