c# Sending keyboard commands to another window / process

后端 未结 3 1371
温柔的废话
温柔的废话 2020-12-01 07:58

I am trying to write a program that will take a line of data and pass it into another window / process.

This is the code I have so far, but I have not been able to w

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 08:35

    [DllImport("user32.dll")]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
    [DllImport("user32.dll")]
    private static extern bool SetForegroundWindow(IntPtr hWnd);
    
    public void Start()
    {
        IntPtr zero = IntPtr.Zero;
        for (int i = 0; (i < 60) && (zero == IntPtr.Zero); i++)
        {
            Thread.Sleep(500);
            zero = FindWindow(null, "YourWindowName");
        }
        if (zero != IntPtr.Zero)
        {
            SetForegroundWindow(zero);
            SendKeys.SendWait("{TAB}");
            SendKeys.SendWait("{TAB}");
            SendKeys.SendWait("{ENTER}");
            SendKeys.Flush();
        }
    }
    

    Try smth like this.

提交回复
热议问题