SendKeys to Putty, MobaXterm etc... Without Hardcoding FindWindow(null, "Command Prompt");

Jo SRM 116 Reputation points
2021-09-29T23:26:33.283+00:00

C#
Visual Studio 2017
WINFORM

I hope you all are well!

I am developing an application that will send the contents of the Clipboard (string) to any terminal (Putty, MobaXterm, etc...). I found a partial solution. It works well if you hard code the windows you want to receive the text. e.g. I hard coded for the Command Prompt windows, and it works.

 IntPtr wordHandle = FindWindow(null, "Command Prompt");  

What I am trying to figure out is how to do this without hard coding which windows gets the text because I do not know what app the user might use. Is there a way to get the last active windows because if I click on the button on the form the other windows loses its focus.

Credit to sending-key-strokes-to-another-application

Code used:

136438-code-used.jpg

Any suggestions is appreciated.
Sincerely and regards,

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,204 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sam of Simple Samples 5,516 Reputation points
    2021-09-30T01:38:13.253+00:00

    It is not clear what you are asking; you are saying many things and I am not sure if any of the extra stuff is part of the question.

    Try the following to get the last active window.

    IntPtr lastWindowHandle = GetWindow(Process.GetCurrentProcess().MainWindowHandle, (uint)GetWindow_Cmd.GW_HWNDNEXT);
    IntPtr hWndParent = GetParent(lastWindowHandle);
    while (!hWndParent.Equals(IntPtr.Zero))
    {
        lastWindowHandle = hWndParent;
        hWndParent = GetParent(lastWindowHandle);
    }
    

    Then after the loop lastWindowHandle will have the handle of the top-level window of the last active application. You will also need the following.

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
    enum GetWindow_Cmd : uint
    {
        GW_HWNDFIRST = 0,
        GW_HWNDLAST = 1,
        GW_HWNDNEXT = 2,
        GW_HWNDPREV = 3,
        GW_OWNER = 4,
        GW_CHILD = 5,
        GW_ENABLEDPOPUP = 6
    }
    [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
    public static extern IntPtr GetParent(IntPtr hWnd);
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,571 Reputation points
    2021-09-30T01:50:51.053+00:00

    You can use GetForegroundWindow to get the active window.

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]  
            static extern IntPtr GetForegroundWindow();  
      
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]  
            static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);  
      
            private static string GetCaptionOfActiveWindow()  
            {  
                var strTitle = string.Empty;  
                var handle = GetForegroundWindow();  
                // Obtain the length of the text     
                var intLength = 256;  
                var stringBuilder = new StringBuilder(intLength);  
                if (GetWindowText(handle, stringBuilder, intLength) > 0)  
                {  
                    strTitle = stringBuilder.ToString();  
                }  
                return strTitle;  
            }  
    

    Combining these codes with Timer should meet your needs.

    You can also consider using SetWinEventHook to monitor EVENT_SYSTEM_FOREGROUND to detect changes in the foreground window. This is an example. I haven't tested it yet, but it should be fine in theory.

    asynchronously GetForegroundWindow via SendMessage or something?


    If the response is helpful, please click "Accept Answer" and upvote it.
    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 comments No comments