首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >接受用户输入的数组方法?

接受用户输入的数组方法?
EN

Stack Overflow用户
提问于 2018-01-17 03:33:19
回答 1查看 73关注 0票数 1

我目前正在做我的一个项目,我叫它“自动语音检测器”,基本上这个程序大部分时间都坐在系统托盘里,只听用户的输入。

我现在得出的结论是,我不能用人们想要的所有命令填充“命令”数组,所以我决定集成一个"AddCommand“用户输入。在那里用户可以自己输入所需的命令,程序稍后将执行我决定的任何操作。然而,我真的需要这方面的帮助。

如何创建一个带有1个参数的字符串数组方法,该参数将是用户输入字符串"command“。将该用户输入添加到字符串数组中。这个是可能的吗?这是我设置的“默认”命令的当前给定代码。

代码语言:javascript
复制
            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);

所以它的工作原理是这样的,只有像commands2这样的其他数组能够接受1个参数,并将其插入到该数组中。下面的代码是整个项目,如果有必要看看。

代码语言:javascript
复制
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");
    }
}

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-17 03:51:36

对你的任务有一些背景知识,我相信你需要一本字典。它将是表单级别的公共变量。关键字将是命令,值将是执行路径。在表单中,您将在分配事件之前使用您的5个值对其进行初始化。

代码语言:javascript
复制
Public Dictionary<String, String> Commands = new Dictionary<String, String>();

所以在表单加载中(你需要5个):

代码语言:javascript
复制
Dictionary.Add("notepad", "notepad.exe");
Dictionary.Add("hotmail", "https://outlook.live.com/owa/");

您将搜索字典,而不是case语句,如果键存在,您将启动该值。假设您有一个名为commands的字典,它将是:

代码语言:javascript
复制
string command = "";
if ( Commands.TryGetValue(e.Result.Text, out command))
    System.Diagnostics.Process.Start(command)

add命令将输入命令名路径和应用程序路径,并添加到字典中。

代码语言:javascript
复制
Commands.Add(commandName, pathToCommand);

请注意,当您这样做时,您还应该保存到用户本地应用程序数据区中的一个文件,该文件可以在表单加载时返回,因此它将被保留,但这超出了范围。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48288564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档