c# - SendMessage send shortcut

Nadeem Bader 21 Reputation points
2021-10-06T21:01:38.537+00:00

Hi
I have an opened RDP instance, and I need to send "Win + Left arrow key" using the SendMessage method:

[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

Assuming, I have the MainWindowHandle object of the RDP instance... I'm not sure which values I need to set in order send the above shortcut.
Can you please assist?

Best Regards,
Nadeem Bader

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,291 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 81,831 Reputation points
    2021-10-06T22:50:33.303+00:00

    You can use keybd_event

    For example (remove the space at Sl eep (editor bug...):

    IntPtr hWnd = FindWindow("TscShellContainerClass", null);
    if (hWnd != IntPtr.Zero)
    {
        ShowWindow(hWnd, SW_SHOWNORMAL);
        SwitchToThisWindow(hWnd, true);
        System.Threading.Thread.Sl eep(200);
        keybd_event(VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY, 0);
        keybd_event(VK_LEFT, 0, 0, 0);
        keybd_event(VK_LEFT, 0, KEYEVENTF_KEYUP, 0);
        keybd_event(VK_LWIN, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
    }
    

    Declarations :

            [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
            [DllImport("User32.dll", SetLastError = true)]
            public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
    
            public const int KEYEVENTF_EXTENDEDKEY = 0x0001;
            public const int KEYEVENTF_KEYUP = 0x0002;
            public const int KEYEVENTF_UNICODE = 0x0004;
            public const int KEYEVENTF_SCANCODE = 0x0008;
    
            public const int VK_LWIN = 0x5B;
            public const int VK_LEFT = 0x25;
            public const int VK_UP = 0x26;
            public const int VK_RIGHT = 0x27;
            public const int VK_DOWN = 0x28;
    
            [DllImport("User32.dll", SetLastError = true)]
            public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    
            public const int SW_HIDE = 0;
            public const int SW_SHOWNORMAL = 1;
            public const int SW_SHOWMINIMIZED = 2;
            public const int SW_SHOWMAXIMIZED = 3;
            public const int SW_RESTORE = 9;
    
            [DllImport("User32.dll", SetLastError = true)]
            public static extern bool SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
    
    1 person found this answer helpful.
    0 comments No comments

  2. Nadeem Bader 21 Reputation points
    2021-10-07T07:10:24.737+00:00

    Hi

    Thank you for reply.
    The idea is restore the mstsc window which is in full screen.
    Seems that the shortcut "Win + Left arrow key" works only after the mstsc window is restored.

    Can you please send me the code which restores the mstsc window using shortcuts?
    Note: I tried the "ShowWindow(hWnd, 9);", but that doesn't work since the mstsc is in full screen.

    Best Regards,
    Nadeem Bader