简体   繁体   English

SendKeys到同一进程的多个

[英]SendKeys to multiple of the same process

I want my program to be able to use "SendKeys.SendWait()", to send keys to multiple of the same process at once. 我希望我的程序能够使用“SendKeys.SendWait()”,一次将密钥发送到同一个进程的多个。 For example if I had x amount of notepad process open, I want it to be able to count the amount then send keys to all of the processes one by one. 例如,如果我打开了x量的记事本进程,我希望它能够计算数量,然后逐个将密钥发送到所有进程。 The program is a console application written in C#. 该程序是一个用C#编写的控制台应用程序。

The code that I have so far: 我到目前为止的代码:

Process[] processes = Process.GetProcessesByName("notepad");
                Process game1 = processes[0];
                do
                {
                    while (!Console.KeyAvailable)
                    {
                            IntPtr p = game1.MainWindowHandle;
                            SetForegroundWindow(p);
                            SendKeys.SendWait("{ESC}");
                            Thread.Sleep(1000);
                            SendKeys.SendWait("^{C}");
                            SendKeys.SendWait("{ENTER}");
                            SendKeys.SendWait("{ENTER}");
                            Thread.Sleep(100);
                            SendKeys.SendWait("{A}");                                   
                    }
                } while (Console.ReadKey(true).Key != ConsoleKey.NumPad1);

So far the application is able to find the notepad and send the keys but it only sends it to ONE process over and over (the same process). 到目前为止,应用程序能够找到记事本并发送密钥,但它只能一遍又一遍地将它发送到一个进程(同一个进程)。 I have tried creating a for loop but I'm not sure how I would count or implement the for loop so I am able to send it to each of the x amount of notepads open. 我已经尝试创建一个for循环,但我不确定如何计算或实现for循环,所以我能够将它发送到每个打开的x个记事本中。

In the code below, I am able to make it so that it sends between 2 notepad processes, but only because I have told it there are 2 and opened 2. 在下面的代码中,我能够使它在2个记事本进程之间发送,但只是因为我告诉它有2个并打开2。

Process[] processes = Process.GetProcessesByName("notepad");
            Process game1 = processes[0];
            do
            {
                while (!Console.KeyAvailable)
                {
                        game1 = processes[0];
                        IntPtr p = game1.MainWindowHandle;
                        SetForegroundWindow(p);
                        SendKeys.SendWait("{ESC}");
                        Thread.Sleep(1000);
                        SendKeys.SendWait("^{C}");
                        SendKeys.SendWait("{ENTER}");
                        SendKeys.SendWait("{ENTER}");
                        Thread.Sleep(100);
                        SendKeys.SendWait("{A}");

                        game1 = processes[1];
                        p = game1.MainWindowHandle;
                        SetForegroundWindow(p);
                        SendKeys.SendWait("{ESC}");
                        Thread.Sleep(1000);
                        SendKeys.SendWait("^{C}");
                        SendKeys.SendWait("{ENTER}");
                        SendKeys.SendWait("{ENTER}");
                        Thread.Sleep(100);
                        SendKeys.SendWait("{A}");
                }
            } while (Console.ReadKey(true).Key != ConsoleKey.NumPad1);

While i do not have the rest of you code to test it, should work if you just change your while loop with a for loop 虽然我没有其余的代码来测试它,但如果你只是用for循环改变你的while循环应该工作

for (int i = 0; i < processes.Length; i++)
        {
            Process game1 = processes[i];
        }

Sorry i cannot edit the code in the comment but basically you need just to loop through your process array starting from the first element at the position 0 and increment the counter after you have notified 1 running process 抱歉,我无法编辑注释中的代码,但基本上您只需要从位置0的第一个元素开始循环处理数组,并在通知1个运行进程后递增计数器

If you want to use the foreach loop you need to do it this way 如果你想使用foreach循环,你需要这样做

 foreach (var temp in processes)
            {
                Process game1 = temp; // the assigment is not necessary you can just use the temp variable 
            }

Basically with foreach you do not need to specify the index because it will "automatically" check each object that is stored inside the array. 基本上使用foreach,您不需要指定索引,因为它将“自动”检查存储在数组中的每个对象。 More details about for vs foreach Difference between foreach and for loops over an IEnumerable class in C# 关于for foreach的更多细节关于C#中IEnumerable类的foreach和for循环之间的区别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM