SendKeys on the WinKeys button

Pixel-ink 20 Reputation points
2023-06-20T14:31:49.97+00:00

I have looked at MS's list of available send keys on their site, but they don't seem to have one listed for the actual Windows Key.

The only solution I found was....

SendKey.Send("^{ESC}")

However, the key strokes to show the desktop is Winkey+D

But, I can't seem to make using the ^{ESC} work with the letter "D"

SendKey.Send("^{ESC}(d)")

All I get is the Start menu popping up.

So, exactly how do I simulate "Winkey+D" with SendKeys???

Any input will be greatly appreciated.

Developer technologies | .NET | Other
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-06-21T03:12:41.9+00:00

    Hi @Pixel-ink ,Welcome to Microsoft Q&A.

    You can use the functions in the user32.dll library to simulate keyboard presses.

    keybd_event function

    The sample code is:

    using System;
    using System.Runtime.InteropServices;
    
    namespace _6_21_x
    {
        internal class Program
        {
            [DllImport("user32.dll")]
            public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);
    
            const int KEYEVENTF_EXTENDEDKEY = 0x0001;
            const int KEYEVENTF_KEYUP = 0x0002;
            const byte VK_LWIN = 0x5B;
            const byte VK_D = 0x44;
            static void Main(string[] args)
            {
                SendWinKeyD();
                Console.ReadLine();
            }
            static void SendWinKeyD()
            {
                keybd_event(VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero); // Press Win key
                keybd_event(VK_D, 0, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero); // Press D key
                keybd_event(VK_D, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, IntPtr.Zero); // Release D key                                                                                       
                keybd_event(VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, IntPtr.Zero); // Release Win key
            }
        }
    }
    

    If there is a problem with computer performance, you can try to add Thread.Sleep(100); between keybd_event.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.