检测用户何时按下shift键

时间:2015-05-22 17:01:42

标签: c# visual-studio-2010 audio piano

目前我正在使用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

2 个答案:

答案 0 :(得分:1)

您应该考虑使用表单的KeyEvents,尤其是KeyDownKeyUp事件。 重要提示:要正确使用该活动,您必须先将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
}