将SendKeys发送到同一进程的多个

时间:2017-05-03 21:34:41

标签: c# process console console-application sendkeys

我希望我的程序能够使用“SendKeys.SendWait()”,一次将密钥发送到同一个进程的多个进程。例如,如果我打开了x量的记事本进程,我希望它能够计算数量,然后逐个将密钥发送到所有进程。该程序是一个用C#编写的控制台应用程序。

到目前为止我的代码:

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);

到目前为止,应用程序能够找到记事本并发送密钥,但它只能一遍又一遍地将它发送到一个进程(同一个进程)。我已经尝试创建一个for循环,但我不确定如何计算或实现for循环,所以我可以将它发送给每个打开的x个记事本。

在下面的代码中,我能够将它发送到两个记事本进程之间,但只是因为我告诉它有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);

1 个答案:

答案 0 :(得分:0)

虽然我没有剩下的代码来测试它,但是如果你只是用for循环改变你的while循环那么应该工作

ng-form class="form-horizontal" name="genderForm">
      <div data-fields="">
        <div class="form-field-gender control-group form-field" data-field="">
          <div class="controls" data-input="">
            <label class="radio">
              <input type="radio" ng-model="appuser.traits[6]" ng-value= "[42198]" name="field-input-gender" id="field-input-gender-0" required>
              <i class="fa fa-male fa-fw"></i> {{genderQuestion.traits[0].value_text}}
            </label>
            <label class="radio">
              <input type="radio" name="field-input-gender" ng-model="appuser.traits[6]" id="field-input-gender-1" ng-value="[42199]" required>
              <i class="fa fa-female fa-fw"></i> {{genderQuestion.traits[1].value_text}}
            </label>
          </div>
          <div class="controls">
            <div class="text-error" data-error="">
            </div>
          </div>
        </div>
      </div>

    </ng-form>

抱歉,我无法编辑注释中的代码,但基本上你只需要从位置0的第一个元素开始循环处理数组,并在通知1个运行进程后递增计数器

如果你想使用foreach循环,你需要这样做

ng-form class="form-horizontal" name="genderForm">
      <div data-fields="">
        <div class="form-field-gender control-group form-field" data-field="">
          <div class="controls" data-input="">
            <label class="radio">
              <input type="radio" ng-model="appuser.traits[{{genderQuestion.id}}]" ng-value= "[{{genderQuestion.traits[0].id}}]" name="field-input-gender" id="field-input-gender-0" required>
              <i class="fa fa-male fa-fw"></i> {{genderQuestion.traits[0].value_text}}
            </label>
            <label class="radio">
              <input type="radio" name="field-input-gender" ng-model="appuser.traits[6]" id="field-input-gender-1" ng-value="[{{genderQuestion.traits[1].id}}]" required>
              <i class="fa fa-female fa-fw"></i> {{genderQuestion.traits[1].value_text}}
            </label>
          </div>
          <div class="controls">
            <div class="text-error" data-error="">
            </div>
          </div>
        </div>
      </div>

    </ng-form>

基本上使用foreach,您不需要指定索引,因为它将“自动”检查存储在数组中的每个对象。关于vs foreach的更多细节 Difference between foreach and for loops over an IEnumerable class in C#