简体   繁体   English

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

[英]How to capture keystroke(s) pressed in certain time period for DataGridView keypress event?

I need to capture keystroke(s) that are pressed in certain time period for example say 300 ms. 我需要捕获在特定时间段内(例如说300毫秒)按下的击键。 So when i press ' a ' and within 300 ms i press ' b ' then i need string " ab ". 因此,当我按下“ a ”并且在300毫秒内按下“ b ”时,我需要字符串“ ab ”。 But if i press key ' c ' after 300 ms of pressed ' b ' then i want " c ". 但是,如果我按下后,“B”的300毫秒按下键“C”然后我想“C”。

I need this for quick jump to DataGridView's cell that starts with quick pressed key(s). 我需要它来快速跳转到以快速按下的键开头的DataGridView的单元格。

I'm not entirely sure I understand your question, but I believe you want a way to execute one block of code if two keys were pressed, or another block of code if three keys were pressed. 我不能完全确定我是否理解您的问题,但是我相信您希望有一种方法,如果按下两个键,则执行一个代码块,或者如果按下三个键,则执行另一个代码块。 Additionally, you want each key press to be within 300ms of one another. 此外,您希望每次按键都在300ms之内。 If I've understood, then this code should do what you want: 如果我已经理解,那么这段代码应该可以满足您的要求:

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!!!";
}

This code assumes a label, lblEventMessage, and something to trigger the KeyUp event (I went with a textbox). 这段代码假设一个标签lblEventMessage,以及一些用来触发KeyUp事件的东西(我和一个文本框一起使用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DataGridView,如何捕获单元格的KeyPress事件C# - DataGridView, how to capture a cell's KeyPress event C# 如何捕获在C#中的DataGridView上触发“CellEndEdit”的击键? - How can I capture the keystroke that triggers “CellEndEdit” on a DataGridView in C#? 如何使用DataGridView的Keydown事件并检查按下了什么键? - How to use the DataGridView's Keydown event and check what key was pressed? 有没有一种方法可以捕获DataGridView KeyPress事件而不显示编辑控件? - Is there a way to capture DataGridView KeyPress event without editing control showing? 如何从datagriview keypress事件执行datagridview cellclick事件? - how to perform datagridview cellclick event from datagriview keypress event? 事件 KeyDown/KeyPress/KeyUp 如何获取按下按钮的名称? - Event KeyDown/KeyPress/KeyUp How to get the name of the pressed button? 如何确定KeyPress事件中是否已按下退格键? - How can I determine if the Backspace has been pressed in the KeyPress event? 如何等待 c# 中的按键一段时间? - how to wait a certain time for a keypress in c#? 如何在DataGridView的KeyPress事件上将焦点设置在TextBox上(以前的焦点在此TextBox上) - How to set focus on TextBox (previously the focus was on this TextBox) on KeyPress event of DataGridView 如何在一段时间内更改变量 - How to change variable for a certain period of time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM