Search code examples
c#sendkeys

Sendkeys for Windows Start key


I'm trying to use the Sendkeys to simulate the Windows start key, but none of the options I tried work, does anybody know how can it be done?

CODE

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private Thread thrTyping;

    private void startThread()
    {
        ThreadStart ts = new ThreadStart(sendKeys);
        thrTyping = new Thread(ts);
        thrTyping.Start();
    }

    private void sendKeys()
    {
       // TEST 1
       Thread.Sleep(5000);
       SendKeys.SendWait("(^)"+"{ESC}");           

       // TEST 2
       Thread.Sleep(5000);
       SendKeys.SendWait("{LWin}");
    }

Solution

  • Use keybd_event instead:

    private const int KEYEVENTF_EXTENDEDKEY = 0x1;
    private const int KEYEVENTF_KEYUP = 0x2;
    
    [DllImport("user32.dll")]
    static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
    
    private static void PressKey(byte keyCode)
    {
        keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
        keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
    }
    

    List of KeyCodes (The one you are looking for is 0x5B - left win key)