-3

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];
}
3
  • 1
    Can you include code or specific examples of what you have tried so far? Can you also be more specific about the problem you are trying to solve? Are you wanting to measure this in your app, across the machine like a keylogger? Is this in a game situation or just a line of business app? Commented Oct 20, 2016 at 16:09
  • Start a stop watch on keydown, end it on keyup. Calculate the difference.
    – dmeglio
    Commented Oct 20, 2016 at 16:10
  • @NicholausLawson i write my code. i want to use this to come forward or backward a movie .
    – saedbfd
    Commented Oct 20, 2016 at 16:21

3 Answers 3

4

This may be an alternative

using System.Diagnostics;
using System.Windows.Forms;

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();

        var watch = new Stopwatch();
        KeyDown += (s, e) => watch.Start();
        KeyUp += (s, e) => { 
            watch.Stop();
            var elapsed = watch.ElapsedMilliseconds;    // Duration
            watch.Reset();
        };
    }
}
2
  • 1
    This is the best, and most accurate alternative! Commented Oct 20, 2016 at 17:43
  • 2
    Good answer, and frankly more helpful than mine if looking for a specific example to learn from.
    – DBPriGuy
    Commented Oct 20, 2016 at 17:52
0

Your code is a good start, though you might need to alter it a bit to see it working.

private void form1_KeyDown(object sender, System.Windows.Forms.KeyPressEventArgs e)
{        
    if (e.Key == Key.Right)
    {
        date_down = DateTime.Now;
    }
    e.Handled = true;
}

private void form1_KeyUp(object sender, System.Windows.Forms.KeyPressEventArgs e)
{ 
    if (e.Key == Key.Right)
    {
        var timeDelta = DateTime.Now - date_down;
    }
}

You can also trigger a timer from this if you want, instead of grabbing current time and evaluating.

1
  • The code in this answer will only work as intended once.
    – DBPriGuy
    Commented Oct 20, 2016 at 17:51
-1

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.

2
  • thanks, but how can i start and stop timer and measure time between this? have you any sample code?
    – saedbfd
    Commented Oct 20, 2016 at 16:19
  • @saedbfd I looked at your edit. Note the the List.Add method will append a value to the end of a list, not the beginning. So in your current implementation, whenever you raise a key, it will compare it to the first time you pressed down a key, not the latest.
    – DBPriGuy
    Commented Oct 20, 2016 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.