Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Speech.Recognition;
- namespace SpeechRecognitionForms
- {
- public partial class Form1 : Form
- {
- public SpeechRecognitionEngine recEngine;
- public static bool keyHold = false;
- NotifyIcon IconPicture;
- Icon ActiveIcon;
- public static Dictionary<string, string> CommandsList { get; set; }
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- #region Icon and windows system tray dropdown text & click events
- //Creating icon and setting it to default.
- ActiveIcon = new Icon("speak_lzW_icon.ico");
- IconPicture = new NotifyIcon();
- IconPicture.Icon = ActiveIcon;
- //iconPicture.Visible = true;
- //Creating menu item for window in system tray.
- MenuItem ProgNameMenuItem = new MenuItem("Voice detection by: Lmannen");
- MenuItem QuitMenuItem = new MenuItem("Quit");
- ContextMenu contextMenu = new ContextMenu();
- contextMenu.MenuItems.Add(ProgNameMenuItem);
- contextMenu.MenuItems.Add(QuitMenuItem);
- //Adding the icon to the system tray window.
- IconPicture.ContextMenu = contextMenu;
- //System tray click event handlers
- QuitMenuItem.Click += QuitMenuItem_Click;
- IconPicture.MouseDoubleClick += IconPicture_MouseDoubleClick1;
- #endregion
- CommandsList = new Dictionary<string, string>();
- #region SpeechRecognition commands & event handlers
- recEngine = new SpeechRecognitionEngine();
- recEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recEngine_SpeechRecognized);
- recEngine.AudioStateChanged += new EventHandler<AudioStateChangedEventArgs>(recEngine_AudioStateChange);
- Choices commands = new Choices();
- commands.Add(CommandsList); // this gives error etc
- GrammarBuilder gBuilder = new GrammarBuilder();
- gBuilder.Append(commands);
- Grammar grammar = new Grammar(gBuilder);
- recEngine.SetInputToDefaultAudioDevice();
- recEngine.LoadGrammarAsync(grammar);
- recEngine.RequestRecognizerUpdate();
- recEngine.RecognizeAsync(RecognizeMode.Multiple);
- #endregion
- }
- internal void recEngine_AudioStateChange(object sender, AudioStateChangedEventArgs e)
- {
- InputStatusLbl.Text = string.Format("{0}", e.AudioState);
- }
- internal static void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
- {
- string command = "";
- if(CommandsList.TryGetValue(e.Result.Text, out command))
- {
- System.Diagnostics.Process.Start(command);
- }
- }
- private void Form1_Resize(object sender, EventArgs e)
- {
- if (WindowState == FormWindowState.Minimized)
- {
- ShowInTaskbar = false;
- ShowIcon = false;
- IconPicture.Visible = true;
- }
- }
- private void IconPicture_MouseDoubleClick1(object sender, MouseEventArgs e)
- {
- ShowInTaskbar = true;
- IconPicture.Visible = false;
- ShowIcon = true;
- WindowState = FormWindowState.Normal;
- }
- private void QuitMenuItem_Click(object sender, EventArgs e)
- {
- IconPicture.Dispose();
- this.Close();
- }
- private void addToolStripMenuItem_Click(object sender, EventArgs e)
- {
- string key = Microsoft.VisualBasic.Interaction.InputBox("Add a voice-command by text", "Command");
- MessageBox.Show(key + " is now added to the command list");
- string value = Microsoft.VisualBasic.Interaction.InputBox("Add path to the command", "Executable path");
- MessageBox.Show(value + " is now added to the commands path");
- CommandsList.Add(key, value);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement