title: control mouse with speech recognition
body: <p>I would like to create an application that controls a mouses location and its left and right clicks using speech recognition.</p>
  
  <p>I have built an Application that can control the mouse pointer <strong>but I am stuck on the code to simulate "left &amp; right" clicks by speech.</strong> </p>
  
  <p>Here is the code I have so far:</p>
  
  <pre><code>private void Initialize()
      {
          recognitionEngine = new SpeechRecognitionEngine();
          recognitionEngine.SetInputToDefaultAudioDevice();
          recognitionEngine.SpeechRecognized += (s, args) =&gt;
          {
              string line = "";
              foreach (RecognizedWordUnit word in args.Result.Words)
              {
                  if (word.Confidence &gt; 0.5f)
                      line += word.Text + " ";
              }
              string command = line.Trim();
              switch (command)
              {
                  case "left":
                      MoveMouse(Cursor.Position.X - 50, Cursor.Position.Y);
                      break;
                  case "right":
                      MoveMouse(Cursor.Position.X + 50, Cursor.Position.Y);
                      break;
                  case "up":
                      MoveMouse(Cursor.Position.X, Cursor.Position.Y - 50);
                      break;
                  case "down":
                      MoveMouse(Cursor.Position.X, Cursor.Position.Y + 50);
                      break;
              }
  
              txtOutput.Text += line;
              txtOutput.Text += Environment.NewLine;
          };
  
          recognitionEngine.UnloadAllGrammars();
          recognitionEngine.LoadGrammar(CreateGrammars());
          recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
      }
  
      private Grammar CreateGrammars()
      {
          Choices commandChoices = new Choices("left", "right", "up", "down");
          GrammarBuilder grammarBuilder = new GrammarBuilder();
          grammarBuilder.Append(commandChoices);
          return new Grammar(grammarBuilder);
      }
  
      private void MoveMouse(int x, int y)
      {
          this.Cursor = new Cursor(Cursor.Current.Handle);
          Cursor.Position = new Point(x, y);
          Cursor.Clip = new Rectangle(this.Location, this.Size);
      }
  }
  }
  </code></pre>