- 1 year ago
Send keyboard strokes to an RemoteApp application within an Remote Desktop session with Winforms C#
To send keyboard strokes to a RemoteApp application within a Remote Desktop session using WinForms and C#, you can make use of the SendKeys
class. Here's an example of how you can achieve this:
csharpusing System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
public class Program
{
// Import the necessary Windows API functions
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern IntPtr SetForegroundWindow(IntPtr hWnd);
// Define a delay between key strokes if needed
private const int Delay = 100;
public static void Main()
{
// Get the handle of the RemoteApp window
IntPtr remoteAppHandle = GetRemoteAppWindowHandle();
// Set the RemoteApp window as the foreground window
SetForegroundWindow(remoteAppHandle);
// Simulate keyboard strokes
SendKeys.SendWait("Hello, World!");
Thread.Sleep(Delay);
SendKeys.SendWait("{ENTER}");
}
private static IntPtr GetRemoteAppWindowHandle()
{
// Implement your logic here to find and return the handle of the RemoteApp window
// You can use the Window title or other identification criteria to find the correct window
// In this example, we'll use the GetForegroundWindow function to get the handle
return GetForegroundWindow();
}
}
In this example, we use the GetForegroundWindow
function from the user32.dll
library to get the handle of the RemoteApp window. Then, we use the SetForegroundWindow
function to set the RemoteApp window as the foreground window so that the key strokes are sent to it. Finally, we use the SendKeys.SendWait
method to simulate the keyboard strokes.
Make sure to replace the GetRemoteAppWindowHandle
method implementation with your own logic to find and return the handle of the RemoteApp window based on your requirements.
Note: This code assumes that you have proper access and permissions to interact with the RemoteApp application within the Remote Desktop session.