Programmatically Opening the Windows Start Menu

windows-vista-start-menu

Some time ago I was looking for a way to launch the Windows Start Menu programmatically. The simplest way I thought so far was to fake the Ctrl + Escape keystroke so the menu could be tricked as being requested by the user.

I was more than satisfied to discover that a System.Windows.Forms.SendKeys class was readily available from the .NET Framework and that it could be used send keystrokes to other applications.  According to MSDN, all I have to do was call SendKeys.Send("^{ESC}") to achieve my goal.

 

Well, but this didn’t work quite well.

Further reading on MSDN revealed the reason. Sending keystrokes only worked in a focused window, and to focus the Start Menu I needed to get its window handle. A possible, but not so trivial or clean operation to be done in a purely managed world.

Because there is no managed method to activate another application, you can either use this class within the current application or use native Windows methods, such as FindWindow and SetForegroundWindow, to force focus on other applications.

This was going to get quite complex, so I decided for another approach, instead. While googling for the SendKeys class, I found about the SendKeys method of the Windows Script Host (WSH) and decided to give it a try.

 

Using Windows Scripting Host to Send Key Messages to Windows

To use the Windows Script Host in your dot-Net application, all you have to do is include a reference in your project to the Windows Script Host Object Model (found under the COM tab of Visual Studio‘s Add Reference dialog) and then import the IWshRuntimeLibrary namespace to your code with the using directive.

After a bit of testing, it was evident the native SendKeys method behaves differently than the Framework way and didn’t require the windows to be previously focused.

 

So I ended up writing the following code, and decided to share in case someone else finds it useful:

 

using System;

using IWshRuntimeLibrary;

 

namespace cSouza.Utils.Windows

{

 

    /// <summary>

    ///   Provides methods for sending

    ///   keystrokes to an application.

    /// </summary>

    public static class SendKeys

    {

 

        /// <summary>

        ///   Sends one or more keystrokes to the active window

        ///   (as if typed on the keyboard).

        /// </summary>

        /// <param name="keys">

        ///   String value indicating the keystroke(s) you want to send.

        /// </param>

        /// <param name="wait">

        ///   Indicates if execution stops until the sent messages

        ///   have been processd by the target application.

        /// </param>

        public static void Send(string keys, bool wait)

        {

            WshShell shell = new WshShellClass();

            object wObj = wait;

 

            shell.SendKeys(keys, ref wObj);

        }

 

        /// <summary>

        ///   Sends one or more keystrokes to the active window

        ///   (as if typed on the keyboard).

        /// </summary>

        /// <param name="keys">

        ///   String value indicating the keystroke(s) you want to send.

        /// </param>

        public static void Send(string keys)

        {

            Send(keys, false);

        }

 

        /// <summary>

        ///   Sends one or more keystrokes to the active window

        ///   (as if typed on the keyboard) and stops execution

        ///   until the sent messages have been processd by the

        ///   target application

        /// </summary>

        /// <param name="keys">

        ///   String value indicating the keystroke(s) you want to send.

        /// </param>

        public static void SendWait(string keys)

        {

            Send(keys, true);

        }

    }

}

using this class, all we have to do to launch the start menu now is call:

cSouza.Utils.Windows.SendKeys.Send("^{ESC}");

 

I hope you have found this useful. Feel free to use this code as you wish. This is public domain. For more detailed information about how the Windows Script Host SendKeys method works, please read this article, Automate tasks with Windows Script Host’s SendKey method, by Greg Shultz.

Leave a Reply

Your email address will not be published. Required fields are marked *