当我在键盘上单击W / A / S / D时,它不会测量单击两个键之间的时间,而是所有时间。这是我的完整代码:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
stopwatch1.Start();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
if (e.KeyChar == (char)Keys.W)
{
textBox1.Text = textBox1.Text + "W (" + stopwatch1.ElapsedMilliseconds + ") + ";
}
else if (e.KeyChar == (char)Keys.A)
{
textBox1.Text = textBox1.Text + "A (" + stopwatch1.ElapsedMilliseconds + ") + ";
}
else if (e.KeyChar == (char)Keys.S)
{
textBox1.Text = textBox1.Text + "S (" + stopwatch1.ElapsedMilliseconds + ") + ";
}
else if (e.KeyChar == (char)Keys.D)
{
textBox1.Text = textBox1.Text + "D (" + stopwatch1.ElapsedMilliseconds + ") + ";
}
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
stopwatch1.Stop();
}
}
}
示例输出:
W (560) + A (634) + S (753) + D (846) + A (944) +
我想要的是例如:
W (560) + A (128) + S (82)
有人可以帮忙吗?
答案 0 :(得分:0)
我很难相信以前没有问过这个问题,但您可以使用stopwatch1.StartNew();
代替stopwatch1.Start();
,也可以stopwatch1.Stop();
之后使用stopwatch1.Reset();
}
问题是当你停止然后再次开始时,之前的值仍然会加载到秒表中。
更多信息:https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx
答案 1 :(得分:0)
我不明白,你的问题意味着什么。 但我认为你的问题与KeyPress方法一起挂起。 您现在必须触发您使用的事件,如下所示: KeyDown i,如果你按下按钮 KeyUp如果您发布 如果你发布,KeyPress也会触发。
你必须碰撞事件。 KeyUp&按键。也许你会尝试这样的方式:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
stopwatch1.Start();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
if (e.KeyChar == (char)Keys.W)
{
textBox1.Text = textBox1.Text + "W (" + stopwatch1.ElapsedMilliseconds + ") + ";
}
else if (e.KeyChar == (char)Keys.A)
{
textBox1.Text = textBox1.Text + "A (" + stopwatch1.ElapsedMilliseconds + ") + ";
}
else if (e.KeyChar == (char)Keys.S)
{
textBox1.Text = textBox1.Text + "S (" + stopwatch1.ElapsedMilliseconds + ") + ";
}
else if (e.KeyChar == (char)Keys.D)
{
textBox1.Text = textBox1.Text + "D (" + stopwatch1.ElapsedMilliseconds + ") + ";
}
stopwatch1.Stop();
}
}
}