I am currently working on a project of mine, i call it "Automated speech detector" Basically this program sits in the system tray most of the time just listening for user input.
I have now come to a conclusion that i will not be able to fill the "command" array with all the commands people want so i have decided i want tointegrate a "AddCommand" user input. Where the user can input a desired command themself and the program will later do whatever i decide it to do. However i really need help with this.
How can i make a string array method that takes 1 argument, the argument will be the userinputs string "command". adding that userinput to the string array. Is this possible? this is my current given code for the "default" commands i have set.
Choices commands = new Choices();
commands.Add(new string[] { "dollar", "euro", "hotmail", "notepad", "outlook", "onedrive", "discord" });
GrammarBuilder gBuilder = new GrammarBuilder();
gBuilder.Append(commands);
Grammar grammar = new Grammar(gBuilder);
So it will work something like this only that the other array like commands2 will be able to take 1 argument and insert that to the array. Code below is the whole project if neccesary to look at.
public partial class Form1 : Form
{
public SpeechRecognitionEngine recEngine;
public static bool keyHold = false;
NotifyIcon IconPicture;
Icon ActiveIcon;
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
#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(new string[] { "dollar", "euro", "hotmail", "notepad", "outlook", "onedrive", "discord" });
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)
{
switch(e.Result.Text)
{
case "notepad":
System.Diagnostics.Process.Start("notepad.exe");
break;
case "hotmail":
System.Diagnostics.Process.Start("https://outlook.live.com/owa/");
break;
case "outlook":
System.Diagnostics.Process.Start("https://outlook.live.com/owa/");
break;
case "ondrive":
System.Diagnostics.Process.Start("https://onedrive.live.com/");
break;
case "discord":
string name = Environment.UserName;
string path = string.Format(@"C:\Users\{0}\AppData\Local\Discord\app-0.0.300\Discord.exe", name);
System.Diagnostics.Process.Start(path);
break;
}
}
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 input = Microsoft.VisualBasic.Interaction.InputBox("Add a voice-command by text", "Command");
MessageBox.Show(input + " is now added to the command list");
}
}
}