【问题标题】:How to capture keystroke(s) pressed in certain time period for DataGridView keypress event?如何捕获 DataGridView 按键事件在特定时间段内按下的按键?
【发布时间】:2013-03-26 19:28:01
【问题描述】:

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

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

【问题讨论】:

  • 我在 google 中搜索但无法获得正确的结果。我知道如何使用单个按键来执行此操作,但我不知道如何捕获快速按下的键。
  • 我认为您可以使用 KeyDown event 捕获这一切,因为您说的是 300 毫秒,所以您还需要一个计时器

标签: c# datagridview keypress


【解决方案1】:

我不完全确定我是否理解您的问题,但我相信您想要一种方法来在按下两个键时执行一个代码块,或者在按下三个键时执行另一个代码块。此外,您希望每次按键的时间间隔在 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 事件的东西(我使用了一个文本框)。

【讨论】:

  • 非常有帮助。谢谢。
猜你喜欢
  • 1970-01-01
  • 2012-08-06
  • 2014-03-28
  • 1970-01-01
  • 1970-01-01
  • 2018-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多