目前我在Windows窗体应用程序钢琴工作。我所做的是,当你按下键盘上的某个键时,它会播放声音(从技术上讲,它强制按下按钮)。现在我要做的是添加当用户在按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
您应该考虑使用表单的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;
Keys
是一个标志枚举,因此您可以使用以下命令测试shift键:
var shiftPressed = keyData.HasFlag(Keys.Shift);
当shift被按下时,你的switch语句将不起作用(因为值不是Keys.A
,例如,它是Keys.Shift|Keys.A
)。你可以先去掉shift,这样你的switch语句就可以工作了:
var keyWithoutShift = keyData & ~Keys.Shift;
switch (keyWithoutShift)
{
// handle keys as normal
}