question

NadeemBader-5701 avatar image
0 Votes"
NadeemBader-5701 asked NadeemBader-5701 commented

c# - SendMessage send shortcut

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

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
1 Vote"
Castorix31 answered

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);
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

NadeemBader-5701 avatar image
0 Votes"
NadeemBader-5701 answered NadeemBader-5701 commented

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

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

When it is Fullscreen, it can be restored with UIAutomation
(can be tested with "Inspect" tool from Microsoft, by simulating a click on the buttons of the top Toolbar (named "BBar"))

0 Votes 0 ·

I cannot use UiAutomation in my case.
Are there any other ideas?

0 Votes 0 ·
Castorix31 avatar image Castorix31 NadeemBader-5701 ·

Yes,
on my OS (Windows 10 1909), it works with :
PostMessage + WM_SYSCOMMAND + SC_RESTORE


1 Vote 1 ·
Show more comments