1

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");
    }
}

}

4
  • 1
    Are you looking for List<T>? Your question is very confusing Commented Jan 16, 2018 at 19:36
  • 1
    It's not really clear to me where you're stuck. You can create a method that takes any argument you want. You know how to "add" a string[] array to a Choices object already. I guess I just don't see what the actual question/problem is here. Have you tried something that hasn't worked in some specific way? Commented Jan 16, 2018 at 19:37
  • stackoverflow.com/questions/1836959/… Commented Jan 16, 2018 at 19:39
  • Hey, ye will i currently have my own "Choices command" object set. Now i want another command object "Choices command2" instead that can take a userinput instead, rathet than me adding every possible command i can come up with. Basicly through the "addtoolstripmenuitem_Click events" i want the users to be able to take the string input and insert it in a " Choices command2". Basicly i have 1 array for my own set default commands and then i have a second array where users can input their assired command. Commented Jan 16, 2018 at 19:45

1 Answer 1

0

Having some background on your task, I believe you need a Dictionary. It will be a public variable at the form level. The key will be the command and the value will be the path of execution. In the form, you'll initialize it with your 5 values BEFORE assigning your events.

Public Dictionary<String, String> Commands = new Dictionary<String, String>();

So in the form load (you'll need 5 of these):

Dictionary.Add("notepad", "notepad.exe");
Dictionary.Add("hotmail", "https://outlook.live.com/owa/");

Instead of a case statement, you will search the dictionary and if the key exists, you will start the value. Assuming you have a dictionary called commands it would be:

string command = "";
if ( Commands.TryGetValue(e.Result.Text, out command))
    System.Diagnostics.Process.Start(command)

The add command will path in the command name and the application path and add to the dictionary.

Commands.Add(commandName, pathToCommand);

Note that when you do this, you should ALSO save to a file in the users local application data area that can be brought back on form load, so its retained, but that's out of scope.

Sign up to request clarification or add additional context in comments.

17 Comments

Hey, Thank you for your help so far : ) , I clearly understand where you are going with this and i think it is a great idea but i just dont know what to declare as the public variable. As far as the rest of the code i think i can manage!
list of commands - Public Dictionary<String, String> Commands = new Dictionary<String, String>();
I did like this. Public partial class Form1 : Form { public static Dictionary<string, string> CommandsList { get; set; } }. Then under form1_load i added this " CommandsList = new Dictionary(string, string>(); . Thats as long as i have come. I hope this is correct but i do not know how i am gonna handle this dictionary in the GrammarBuilder? here is the full code so you can see how it looks like. pastebin.com/tXPPCK8W
Do i even need to use this anymore? Choices commands = new Choices(); commands.Add(commands); GrammarBuilder gBuilder = new GrammarBuilder(); gBuilder.Append(commands); Grammar grammar = new Grammar(gBuilder);
Yes...because when you add a command it needs to go into your grammar as well
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.