[英]Send key selected in combobox
我填寫這樣的組合框:
foreach (Keys key in Enum.GetValues(typeof(Keys)))
{
comboKey.Items.Add(key);
}
以后,用戶可以選擇MIDI音符和琴鍵。 演奏所選音符時應模擬該琴鍵。 我用SendKeys.Wait
嘗試過
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()
,所以正確的方法是什么?
SendKeys
期望的輸入( .SendWait
或.Send
)並不總是與所按下鍵的名稱匹配。 您可以在此鏈接中找到包含所有“特殊鍵”的列表。 您必須創建一種方法來將comboKey
的名稱轉換為SendKeys
期望的格式。 一種簡單有效的解決方案是依靠Dictionary
。 樣例代碼:
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(selectedKey.ToString());
變成:
SendKeys.SendWait(dict[selectedKey.ToString()]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.