Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How do I open the start menu in C#? Please help me
Posted
Updated 8-Dec-09 10:25am
v2

Add this in your header:
using System.Runtime.InteropServices;


Place the variable declarations on the class level :
[DllImport("User32")]
private static extern int keybd_event(Byte bVk, Byte bScan, long dwFlags, long dwExtraInfo);
private const byte UP = 2;
private const byte CTRL = 17;
private const byte ESC = 27;


Finally on the event where you want to open start menu use :
// Press Ctrl-Esc key to open Start menu
keybd_event(CTRL, 0, 0, 0);
keybd_event(ESC, 0, 0, 0);

// Need to Release those two keys
keybd_event(CTRL, 0, UP, 0);
keybd_event(ESC, 0, UP, 0);


The start menu will open.
 
Share this answer
 
This can be done by sending "Ctrl+Esc" to the system as if it was generated on the keyboard by pressing the Windows key (or Ctrl+Esc on legacy keyboards).

private static void ShowStartMenu()
{
   // key down event:
   const byte keyControl = 0x11;
   const byte keyEscape = 0x1B;
   keybd_event(keyControl, 0, 0, UIntPtr.Zero);
   keybd_event(keyEscape, 0, 0, UIntPtr.Zero);

   // key up event:
   const uint KEYEVENTF_KEYUP = 0x02;
   keybd_event(keyControl, 0, KEYEVENTF_KEYUP, UIntPtr.Zero);
   keybd_event(keyEscape, 0, KEYEVENTF_KEYUP, UIntPtr.Zero);
}

[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,
   UIntPtr dwExtraInfo);


This solution was developed using the documentation found at:
Keyboard Events Simulation using keybd_event() function[^].

The PInvoke signature was otained from www.pinvoke.net.
 
Share this answer
 
Thanks for referencing your code. It was really helpful. Now i can do for other keys too.
 
Share this answer
 
Comments
[no name] 22-Aug-12 14:26pm    
You should never bring threads back from the dead!

The last post from this was in '09!!! Nearly 3 years ago!


If you want to thank the ones that answered it you could have either left a comment or sent them a message (if they have it enabled). Or give them a 5 as that is what it is for.

In no circumstance does it make sense for you to add a solution thus reincarnating this question from the dead.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900