1

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

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

4

1 回答 1

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 事件的东西(我使用了一个文本框)。

于 2013-03-26T19:58:59.827 回答