-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];
}
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
saedbfd
  • 39
  • 2
  • 9
  • 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? – Nicholaus Lawson Oct 20 '16 at 16:09
  • Start a stop watch on keydown, end it on keyup. Calculate the difference. – dmeglio Oct 20 '16 at 16:10
  • @NicholausLawson i write my code. i want to use this to come forward or backward a movie . – saedbfd Oct 20 '16 at 16:21

3 Answers3

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();
        };
    }
}
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
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

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.

DBPriGuy
  • 294
  • 1
  • 13
  • thanks, but how can i start and stop timer and measure time between this? have you any sample code? – saedbfd Oct 20 '16 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 Oct 20 '16 at 16:26