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()]);