2017-04-14 26 views
0

キーパッドでW/A/S/Dをクリックすると、クリックされた2つのキーの間の時間は測定されません。ここに私の完全なコードは次のとおりです。クリックしたキー間の測定時間

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

私はこれは前に頼まれていないと信じ苦労してんだけど、あなたの代わりにstopwatch1.Start();stopwatch1.StartNew();を使用することができますいずれか、またはstopwatch1.Stop();後、あなたが停止してから再起動したときに問題があるstopwatch1.Reset();

使用することができます、以前の値はまだストップウォッチにロードされています。

さらに詳しい情報:https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx

0

私は、あなたの質問で何をあなたの平均を理解していませんでした。 しかし、私はあなたの問題がKeyPressメソッドと一緒にハングアップしていると思っています。 KeyDown iボタンを押した場合 離した場合のKeyUp KeyPressは、離した場合でもトリガーとなります。

あなたは衝突するイベントが必要です。 KeyUp & KeyPress。多分このような方法で試してみてください:

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

私はあなたが衝突した2つの出来事があることを意味しています... –

関連する問題