-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyStrokeRecorder.cs
73 lines (67 loc) · 2 KB
/
KeyStrokeRecorder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
namespace CSharp_Analyzer_of_Keystroke_Dynamics
{
/// <summary>
/// Records keys being pressed and released
/// </summary>
class KeyStrokeRecorder
{
/// <summary>
/// Tracks the time
/// </summary>
private readonly Stopwatch _sw = new Stopwatch();
/// <summary>
/// Contains user pressed keys and corresponding press (release) time
/// </summary>
private readonly Dictionary<Keys, KeyStroke> _dict = new Dictionary<Keys, KeyStroke>();
public List<KeyStroke> KeyStrokes { get; set; }
public KeyStrokeRecorder()
{
KeyStrokes = new List<KeyStroke>();
}
/// <summary>
/// Returns true if the key hase been already pressed. Returns false if the key has not been pressed yet.
/// </summary>
/// <param name="k">Key</param>
/// <returns></returns>
public bool IsKeyPressed(Keys k)
{
return _dict.ContainsKey(k);
}
/// <summary>
/// Adds pressed key in dictionary of the pressed keys
/// </summary>
/// <param name="k">Key</param>
public void KeyPressed(Keys k)
{
_dict.Add(k, new KeyStroke { Pressed = _sw.Elapsed, Key = k });
}
/// <summary>
/// Removes released key from the dictionary of the pressed keys
/// </summary>
/// <param name="k"></param>
public void KeyReleased(Keys k)
{
var ks = _dict[k];
_dict.Remove(k);
ks.Released = _sw.Elapsed;
KeyStrokes.Add(ks);
}
/// <summary>
/// Starts the stopwatch
/// </summary>
public void Start()
{
_sw.Restart();
}
/// <summary>
/// Stops the stopwatch
/// </summary>
public void Stop()
{
_sw.Stop();
}
}
}