Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Speech.Recognition;
  11.  
  12. namespace SpeechRecognitionForms
  13. {
  14.  
  15. public partial class Form1 : Form
  16. {
  17. public SpeechRecognitionEngine recEngine;
  18. public static bool keyHold = false;
  19.  
  20. NotifyIcon IconPicture;
  21. Icon ActiveIcon;
  22.  
  23. public static Dictionary<string, string> CommandsList { get; set; }
  24.  
  25. public Form1()
  26. {
  27. InitializeComponent();
  28.  
  29. }
  30.  
  31. private void Form1_Load(object sender, EventArgs e)
  32. {
  33. #region Icon and windows system tray dropdown text & click events
  34. //Creating icon and setting it to default.
  35. ActiveIcon = new Icon("speak_lzW_icon.ico");
  36. IconPicture = new NotifyIcon();
  37. IconPicture.Icon = ActiveIcon;
  38. //iconPicture.Visible = true;
  39.  
  40. //Creating menu item for window in system tray.
  41. MenuItem ProgNameMenuItem = new MenuItem("Voice detection by: Lmannen");
  42. MenuItem QuitMenuItem = new MenuItem("Quit");
  43. ContextMenu contextMenu = new ContextMenu();
  44. contextMenu.MenuItems.Add(ProgNameMenuItem);
  45. contextMenu.MenuItems.Add(QuitMenuItem);
  46.  
  47. //Adding the icon to the system tray window.
  48. IconPicture.ContextMenu = contextMenu;
  49.  
  50. //System tray click event handlers
  51. QuitMenuItem.Click += QuitMenuItem_Click;
  52. IconPicture.MouseDoubleClick += IconPicture_MouseDoubleClick1;
  53. #endregion
  54.  
  55. CommandsList = new Dictionary<string, string>();
  56.  
  57. #region SpeechRecognition commands & event handlers
  58. recEngine = new SpeechRecognitionEngine();
  59. recEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recEngine_SpeechRecognized);
  60. recEngine.AudioStateChanged += new EventHandler<AudioStateChangedEventArgs>(recEngine_AudioStateChange);
  61.  
  62. Choices commands = new Choices();
  63. commands.Add(CommandsList); // this gives error etc
  64. GrammarBuilder gBuilder = new GrammarBuilder();
  65. gBuilder.Append(commands);
  66. Grammar grammar = new Grammar(gBuilder);
  67.  
  68. recEngine.SetInputToDefaultAudioDevice();
  69. recEngine.LoadGrammarAsync(grammar);
  70. recEngine.RequestRecognizerUpdate();
  71. recEngine.RecognizeAsync(RecognizeMode.Multiple);
  72. #endregion
  73. }
  74.  
  75. internal void recEngine_AudioStateChange(object sender, AudioStateChangedEventArgs e)
  76. {
  77. InputStatusLbl.Text = string.Format("{0}", e.AudioState);
  78. }
  79.  
  80. internal static void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
  81. {
  82. string command = "";
  83. if(CommandsList.TryGetValue(e.Result.Text, out command))
  84. {
  85. System.Diagnostics.Process.Start(command);
  86. }
  87.  
  88. }
  89.  
  90. private void Form1_Resize(object sender, EventArgs e)
  91. {
  92. if (WindowState == FormWindowState.Minimized)
  93. {
  94. ShowInTaskbar = false;
  95. ShowIcon = false;
  96. IconPicture.Visible = true;
  97.  
  98. }
  99. }
  100.  
  101. private void IconPicture_MouseDoubleClick1(object sender, MouseEventArgs e)
  102. {
  103. ShowInTaskbar = true;
  104. IconPicture.Visible = false;
  105. ShowIcon = true;
  106. WindowState = FormWindowState.Normal;
  107. }
  108.  
  109. private void QuitMenuItem_Click(object sender, EventArgs e)
  110. {
  111. IconPicture.Dispose();
  112. this.Close();
  113. }
  114.  
  115.  
  116.  
  117. private void addToolStripMenuItem_Click(object sender, EventArgs e)
  118. {
  119. string key = Microsoft.VisualBasic.Interaction.InputBox("Add a voice-command by text", "Command");
  120. MessageBox.Show(key + " is now added to the command list");
  121.  
  122. string value = Microsoft.VisualBasic.Interaction.InputBox("Add path to the command", "Executable path");
  123. MessageBox.Show(value + " is now added to the commands path");
  124.  
  125. CommandsList.Add(key, value);
  126.  
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement