如何捕获DataGridView按键事件在特定时间段内按下的按键?

时间:2013-03-26 19:28:01

标签: c# datagridview keypress

我需要捕获在特定时间段内按下的按键,例如300 ms。所以当我按下“ a ”并在300毫秒内按“ b ”然后我需要字符串“ ab ”。但是如果我在按下' b '300毫秒后按“ c ”键,那么我想要“ c ”。

我需要这个来快速跳转到以快速按键开头的DataGridView单元格。

1 个答案:

答案 0 :(得分:2)

我不完全确定我理解你的问题,但我相信你想要一种方法来执行一个代码块,如果按下两个键,或者如果按下三个键则需要另一个代码块。此外,您希望每次按键的距离都在300毫秒之内。如果我已经理解,那么这段代码应该做你想要的:

private System.Diagnostics.Stopwatch Watch = new System.Diagnostics.Stopwatch();
private string _KeysPressed;
public string KeysPressed
{
    get { return _KeysPressed; }
    set 
    {
        Watch.Stop();
        if (Watch.ElapsedMilliseconds < 300)
            _KeysPressed += value;
        else
            _KeysPressed = value;
        Watch.Reset();
        Watch.Start();
    }
}        
private void KeyUpEvent(object sender, KeyEventArgs e)
{
    KeysPressed = e.KeyCode.ToString();
    if (KeysPressed == "AB")
        lblEventMessage.Text = "You've pressed A B";
    else if (KeysPressed == "ABC")
        lblEventMessage.Text = "You've pressed A B C";
    else
        lblEventMessage.Text = "C-C-C-COMBOBREAKER!!!";
}

此代码假定一个标签lblEventMessage,以及触发KeyUp事件的东西(我使用了文本框)。