2017-04-14 5 views
0

Wenn ich w/A/S/D auf keyobard klicke, misst es nicht die Zeit zwischen zwei geklicktem Schlüssel, sondern die ganze Zeit. Hier ist meine komplette Code:Messzeit zwischen den Tasten geklickt

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(); 
      } 
     } 
    } 

Beispiel Ausgabe:

W (560) + A (634) + S (753) + D (846) + A (944) + 

Und was ich will ist zum Beispiel:

W (560) + A (128) + S (82) 

Kann jemand helfen?

Antwort

0

Ich bin eine harte Zeit zu glauben, dies wurde vorher nicht gefragt, aber Sie entweder stopwatch1.StartNew(); statt stopwatch1.Start(); oder nach stopwatch1.Stop(); nutzen könnten könnten Sie stopwatch1.Reset();

Das Problem verwenden ist, wenn Sie stoppen und dann wieder von vorn anfangen , der vorherige Wert ist immer noch in der Stoppuhr geladen.

Weitere Informationen: https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx

0

Ich verstehe nicht, was Ihr Mittelwert mit Ihrer Frage. Aber ich denke, dass Ihr Problem mit der KeyPress-Methode zusammenhängt. Sie müssen jetzt, dass die Ereignisse, die Sie verwenden, wie folgt ausgelöst werden: KeyDown ich, wenn Sie die Taste drücken Down KeyUp, wenn Sie und KeyPress Trigger auch freigeben, wenn Sie freigeben.

Sie müssen Ereignisse kollidieren. KeyUp & KeyPress. Vielleicht versuchst du es so:

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(); 
      } 
     } 
    } 
+0

Ich meinte du hast * zwei Ereignisse, die kollidieren ... –

Verwandte Themen