saedbfd saedbfd - 1 month ago 7
C# Question

c# Measuring interval between keyboard keydown and keyup?

I want to measure time (seconds) when a user presses the right arrow key on the keyboard and pick it up.

I have researched about this and find some page but there is not a good answer.

How can I measure this time?

my code

I define a List in top of class:

List<int> date_down = new List<int>();


when user press right arrow key : (in Form_KeyDown method)

if (Keyboard.IsKeyDown(Key.Right))
{
date_down.Add(DateTime.Now.Second);
}


when user press right arrow key and held down times Add into list

when user pick up key (in Form_keyUp method)

if (e.Key == Key.Right)
{
var second = DateTime.Now.Second - date_down[0];
}

Answer

You could start with looking up various classes for measuring time in C#, such as this one.

You will also need a class for detecting key down and key up. Perhaps this might help.

Beyond this, the implementation should be fairly simple. Start the timer when the key is pressed down, stop the timer when the key is released, and use that time as the measurement between when the two events.

Comments