目前我正在使用Windows Form Application中的钢琴。我所做的是当你按下键盘上的某个键时它会发出声音(技术上它会强制按下按钮)。现在我要做的是补充一点,当用户按下一个键并按下shift键时,它会发出更长的声音。例如,如果按下A键,它将播放C和弦的声音,如果按下A +按下shift键,它将播放较长版本的声音。这是按键的代码和播放声音之一的示例:
private bool shiftPressed = false;
const int NOTE_LENGTH = 500;
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData)
{
case Keys.A://C
C_Low.BackColor = Color.Gray;
C_Low.PerformClick();
break;
case Keys.S://D
D_Low.BackColor = Color.Gray;
D_Low.PerformClick();
break;
case Keys.D://E
E_Low.BackColor = Color.Gray;
E_Low.PerformClick();
break;
case Keys.F://F
F_Low.BackColor = Color.Gray;
F_Low.PerformClick();
break;
case Keys.G://F
G_Low.BackColor = Color.Gray;
G_Low.PerformClick();
break;
case Keys.H://A
A_Low.BackColor = Color.Gray;
A_Low.PerformClick();
break;
case Keys.J://B
B_Low.BackColor = Color.Gray;
B_Low.PerformClick();
break;
case Keys.W://C#
C_Diez_Low.BackColor = Color.Gray;
C_Diez_Low.PerformClick();
break;
case Keys.E://D#
D_Diez_Low.BackColor = Color.Gray;
D_Diez_Low.PerformClick();
break;
case Keys.T://F#
F_Diez_Low.BackColor = Color.Gray;
F_Diez_Low.PerformClick();
break;
case Keys.Y://G#
G_Diez_Low.BackColor = Color.Gray;
G_Diez_Low.PerformClick();
break;
case Keys.U://A#
A_Diez_Low.BackColor = Color.Gray;
A_Diez_Low.PerformClick();
break;
}
return base.ProcessCmdKey(ref msg, keyData);
}
例如C_Low_Click_1
事件:
private void C_Low_Click_1(object sender, EventArgs e) //Play C low
{
if (shiftPressed)
{
C_Low.BackColor = Color.White;
if (Low.Checked)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(Properties.Resources.C_Low_Long);
player.Load();
player.PlaySync();
}
if (Med.Checked)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(Properties.Resources.C_Medium_Long);
player.Load();
player.PlaySync();
}
if (High.Checked)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(Properties.Resources.C_High_Long);
player.Load();
player.PlaySync();
}
}
else
{
C_Low.BackColor = Color.White;
if (Low.Checked)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(Properties.Resources.C_Low);
player.Load();
player.PlaySync();
}
if (Med.Checked)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(Properties.Resources.C_Medium);
player.Load();
player.PlaySync();
}
if (High.Checked)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(Properties.Resources.C_High);
player.Load();
player.PlaySync();
}
}
}
任何帮助将不胜感激,如果我没有提供任何信息,请告诉我:D
答案 0 :(得分:1)
您应该考虑使用表单的KeyEvents
,尤其是KeyDown
和KeyUp
事件。 重要提示:要正确使用该活动,您必须先将KeyPreview
属性设置为true
!
然后在KeyDown
事件中使用以下代码:
shiftPressed = e.Shift;
并在KeyUp
事件中:
shiftPressed = false;
// also possible, although I won't recommend using the code below,
// as there could be some incorrect handling if shift is pressed
// shiftPressed = e.Shift;
答案 1 :(得分:1)
Keys
是一个标志枚举,因此您将使用以下方法测试shift键:
var shiftPressed = keyData.HasFlag(Keys.Shift);
当按下shift时,您的开关语句不起作用(因为值不是Keys.A
,例如,它是Keys.Shift|Keys.A
)。您可以先移除班次,以便切换语句起作用:
var keyWithoutShift = keyData & ~Keys.Shift;
switch (keyWithoutShift)
{
// handle keys as normal
}