Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在组合框中选择发送键_C#_Combobox_Sendkeys - Fatal编程技术网

C# 在组合框中选择发送键

C# 在组合框中选择发送键,c#,combobox,sendkeys,C#,Combobox,Sendkeys,我填充一个组合框,如下所示: foreach (Keys key in Enum.GetValues(typeof(Keys))) { comboKey.Items.Add(key); } 稍后,用户可以选择MIDI音符和键。播放所选音符时应模拟该键。我用SendKeys试过了。等等 public void NoteOn(NoteOnMessage msg) //Is fired when a MIDI note us catched { AppendText

我填充一个组合框,如下所示:

foreach (Keys key in Enum.GetValues(typeof(Keys)))
{
    comboKey.Items.Add(key);
}
稍后,用户可以选择MIDI音符和键。播放所选音符时应模拟该键。我用SendKeys试过了。等等

public void NoteOn(NoteOnMessage msg) //Is fired when a MIDI note us catched 
    {
        AppendTextBox(msg.Note.ToString());
        if (chkActive.Checked == true)
        {
            if (comboKey != null && comboNote != null)
            {
                Note selectedNote = Note.A0;

                this.Invoke((MethodInvoker)delegate()
                {
                    selectedNote = (Note)comboNote.SelectedItem;
                });

                if (msg.Note == selectedNote)
                {
                    Keys selectedKey = Keys.A; //this is just so I can use the variable

                    this.Invoke((MethodInvoker)delegate()
                    {
                        selectedKey = (Keys)comboKey.SelectedItem;
                    });

                    SendKeys.SendWait(selectedKey.ToString());


                }
            }
        }
    }

但是,例如,如果我在组合框中选择“空格”键并播放所需的音符,它不会生成空格,而只是写入“空格”。我知道这可能是因为我写了
selectedKey.ToString()
,那么正确的方法是什么?

发送键所期望的输入(
.SendWait
.Send
)并不总是与按下的键的名称匹配。您可以在中找到包含所有“特殊键”的列表。您必须创建一种方法,将
comboKey
中的名称转换为
SendKeys
所需的格式。一个简单而有效的解决方案是依靠
字典
。示例代码:

Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("a", "a");
dict.Add("backspace", "{BACKSPACE}"); 
dict.Add("break", "{BREAK}");
//replace the keys (e.g., "backspace" or "break") with the exact name (in lower caps) you are using in comboKey
//etc.
SendKeys.SendWait(dict[selectedKey.ToString()]);