[英]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.