public async void PerformCommand(SpeechRecognizedEventArgs e, AvailableCommandsForm form, SpeechRecognizer speechRecogniser, DictateSpeech dictateSpeech, Microsoft.CognitiveServices.Speech.SpeechRecognizer speechRecognizer) { UpdateCurrentProcess(); try { SpeechUI.SendTextFeedback(e.Result, $"Recognised: {e.Result.Text} {e.Result.Confidence:P1}", true); } catch (Exception exception) { AutoClosingMessageBox.Show(exception.Message, "Error Sending feedback to speech recognition", 3000); } if (e.Result.Grammar.Name == "New with Space" && e.Result.Confidence > 0.6) { inputSimulator.Keyboard.TextEntry(" new "); inputSimulator.Keyboard.KeyDown(VirtualKeyCode.ESCAPE); } else if (e.Result.Grammar.Name == "Step Over" && e.Result.Confidence > 0.6) { inputSimulator.Keyboard.KeyDown(VirtualKeyCode.F10); } else if (e.Result.Grammar.Name == "Step Into" && e.Result.Confidence > 0.6) { inputSimulator.Keyboard.KeyDown(VirtualKeyCode.F11); } else if (e.Result.Grammar.Name == "Reset Code" && e.Result.Confidence > 0.6) { inputSimulator.Keyboard.ModifiedKeyStroke(VirtualKeyCode.SHIFT, VirtualKeyCode.F5); } else if (e.Result.Grammar.Name == "Use Dragon" && e.Result.Confidence > 0.6) { ToggleSpeechRecognitionListeningMode(inputSimulator); inputSimulator.Keyboard.KeyDown(VirtualKeyCode.ADD); } else if (e.Result.Grammar.Name == "Window Monitor Switch" && e.Result.Confidence > 0.6) { inputSimulator.Keyboard.ModifiedKeyStroke(windowAndShift, VirtualKeyCode.RIGHT); } else if (e.Result.Grammar.Name == "Select Line" && e.Result.Confidence > 0.6) { inputSimulator.Keyboard.KeyPress(VirtualKeyCode.HOME); inputSimulator.Keyboard.ModifiedKeyStroke(VirtualKeyCode.SHIFT, VirtualKeyCode.END); } else if (e.Result.Grammar.Name == "Mouse Down" && e.Result.Confidence > 0.6) { inputSimulator.Mouse.LeftButtonDown(); } else if (e.Result.Grammar.Name == "Shutdown Windows" && e.Result.Confidence > 0.5) { CommandToBeConfirmed = e.Result.Grammar.Name; SetupConfirmationCommands(speechRecogniser, form); } else if (e.Result.Grammar.Name == "Restart Windows" && e.Result.Confidence > 0.5) { CommandToBeConfirmed = e.Result.Grammar.Name; SetupConfirmationCommands(speechRecogniser, form); } else if (e.Result.Grammar.Name == "Confirmed") { if (CommandToBeConfirmed == "Shutdown Windows") { Process.Start("shutdown", "/s /t 10"); } else if (CommandToBeConfirmed == "Restart Windows") { Process.Start("shutdown", "/r /t 10"); } PerformQuitApplicationCommand(e); } else if (e.Result.Grammar.Name == "Short Dictation" && e.Result.Confidence > 0.4) { await PerformShortDictation(e, form, dictateSpeech, speechRecognizer); } else if (e.Result.Grammar.Name == "Serenade" && e.Result.Confidence > 0.4) { PerformSerenadeCommand(speechRecogniser); } else if (e.Result.Grammar.Name == "Denied") { var availableCommands = speechSetup.SetUpMainCommands(speechRecogniser, form.UseAzureSpeech); form.RichTextBoxAvailableCommands = availableCommands; } else if (e.Result.Grammar.Name == "Studio" && e.Result.Confidence > 0.5) { RunVisualStudioCommand(speechRecogniser); } else if (e.Result.Grammar.Name == "Default Box" && e.Result.Confidence > 0.5) { Process.Start(@"C:\Users\MPhil\Source\Repos\SpeechRecognitionHelpers\DictationBoxMSP\bin\Release\DictationBoxMSP.exe"); } else if (e.Result.Grammar.Name == "Dictation Box" && e.Result.Confidence > 0.5) { Process.Start(@"C:\Program Files (x86)\Speech Productivity\dictation box default\dictation box.exe"); } else if (e.Result.Grammar.Name == "Get and Set" && e.Result.Confidence > 0.5) { inputSimulator.Keyboard.TextEntry(" { get; set; }"); } else if (e.Result.Grammar.Name.Contains("Phonetic Alphabet")) // Could be lower, mixed or upper { ProcessKeyboardCommand(e); } else if (e.Result.Grammar.Name == "Show Recent" && e.Result.Confidence > 0.5) { inputSimulator.Keyboard.ModifiedKeyStroke(VirtualKeyCode.LMENU, VirtualKeyCode.VK_F); inputSimulator.Keyboard.KeyPress(VirtualKeyCode.VK_J); } else if (e.Result.Grammar.Name == "Fresh Line" && e.Result.Confidence > 0.5) { inputSimulator.Keyboard.KeyPress(VirtualKeyCode.END); inputSimulator.Keyboard.KeyPress(VirtualKeyCode.RETURN); } else if (e.Result.Grammar.Name == "Semi Colon" && e.Result.Confidence > 0.3) { inputSimulator.Keyboard.TextEntry(";"); } // where the grammar name is the same Click cancelas the method Without the perform and command, with the spaces remove use reflection to call it else if (e.Result.Confidence > 0.6) { string methodName = $"Perform{e.Result.Grammar.Name.Replace(" ", "")}Command"; Type thisType = this.GetType(); //MethodInfo theMethod = thisType.GetMethod(methodName,BindingFlags.NonPublic | BindingFlags.Instance); try { thisType.InvokeMember(methodName, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod , null, this, new Object[] { e }); } catch (Exception exception) { //AutoClosingMessageBox.Show(exception.Message, $"Error Running a method {exception.Source}", 3000); System.Windows.Forms.MessageBox.Show(exception.Message, "Error running a method", MessageBoxButtons.OK); } } }
private async Task PerformShortDictation(SpeechRecognizedEventArgs e, AvailableCommandsForm form, DictateSpeech dictateSpeech, Microsoft.CognitiveServices.Speech.SpeechRecognizer azureSpeechRecogniser) { ToggleSpeechRecognitionListeningMode(inputSimulator); var result = await dictateSpeech.RecognizeSpeechAsync(azureSpeechRecogniser); form.TextBoxResults = result.Text; var rawResult = result.Text; if (!e.Result.Text.ToLower().Contains("punctuation")) { rawResult = RemovePunctuation(rawResult); } string[] stringSeparators = new string[] { " " }; List <string> words = rawResult.Split(stringSeparators, StringSplitOptions.None).ToList(); if (e.Result.Text.ToLower().Contains("camel")) { var counter = 0; string value = ""; foreach (var word in words) { counter++; if (counter != 1) { value = value + word.Substring(0, 1).ToUpper() + word.Substring(1).ToLower(); } else { value += word.ToLower(); } rawResult = value; } } else if (e.Result.Text.ToLower().Contains("variable")) { string value = ""; foreach (var word in words) { if (word.Length > 0) { value = value + word.Substring(0, 1).ToUpper() + word.Substring(1).ToLower(); } } rawResult = value; } else if (e.Result.Text.ToLower().Contains("dot notation")) { string value = ""; foreach (var word in words) { if (word.Length > 0) { value = value + word.Substring(0, 1).ToUpper() + word.Substring(1).ToLower() + "."; } } rawResult = value.Substring(0, value.Length - 1); } else if (e.Result.Text.ToLower().Contains("title")) { string value = ""; foreach (var word in words) { if (word.Length > 0) { value = value + word.Substring(0, 1).ToUpper() + word.Substring(1).ToLower() + " "; } } rawResult = value; } else if (e.Result.Text.ToLower().StartsWith("upper")) { string value = ""; foreach (var word in words) { value = value + word.ToUpper() + " "; } rawResult = value; } else if (e.Result.Text.ToLower().StartsWith("lower")) { string value = ""; foreach (var word in words) { value = value + word.ToLower() + " "; } rawResult = value; } if (!e.Result.Text.ToLower().StartsWith("short") && e.Result.Text.ToLower() != "dictation") { rawResult = rawResult.Trim(); } if (rawResult.Length > 0) { inputSimulator.Keyboard.TextEntry(rawResult); } ToggleSpeechRecognitionListeningMode(inputSimulator); }