// Called when speech is recognised private static void Recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { int iconIndex = GetSuitableIconIndex(e.Result); if (iconIndex != -1) { NamedDesktopPoint icon = DesktopIcons[iconIndex]; Console.WriteLine(icon.Name); Position iconPosition = new Position(icon.X + 20, icon.Y + 50); // The image numbers correspond exactly to the order of positions in the positions array so it can be used to get the correct image. int imageIndex = ClosestImage.UseClosest(ScreenSize, iconPosition, Positions); // Idea for this came from this https://pointerpointer.com where it points to your mouse. // I'm just using all the images stored there and the positions (it's much easier) Image image = GetImageFromURL($"https://pointerpointer.com/images/{imageIndex}.jpg"); string tempPath = Path.Combine(Path.GetTempPath(), "pointerwallpaper.bmp"); // Save the file as a bmp so it can be set as a background image.Save(tempPath, ImageFormat.Bmp); // Calls the Win32 API to change the desktop background Win32.SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, tempPath, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE); } }
// Tries to find the an icon that best suits what the user spoke. private static int GetSuitableIconIndex(RecognitionResult result) { string text = result.Text; text = RemoveSpecialChars(text).ToLower(); // Loop through all the icons for (int i = 0; i < DesktopIcons.Length; i++) { NamedDesktopPoint icon = DesktopIcons[i]; string iconNameLower = icon.Name.ToLower(); // This removes the file extension because saying the file extension would be very hard to recognise if (iconNameLower.LastIndexOf('.') > 0) { iconNameLower = iconNameLower.Substring(0, iconNameLower.LastIndexOf('.')); } iconNameLower = RemoveSpecialChars(iconNameLower); // If the icon name perfectly matches just return the icon but this probably won't happen so we need to use homophones if (iconNameLower.Contains(text)) { return(i); } // Loop through all the homophones in the speech result foreach (RecognizedPhrase phrase in result.Homophones) { // This will most likely return the icon at some point because the homophones are just // words/sentences the speech recognition thinks you might have said, but wasn't 100% sure. if (iconNameLower.Contains(RemoveSpecialChars(phrase.Text.ToLower()))) { return(i); } } } // -1 means nothing was found return(-1); }