-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMonitor.cs
159 lines (138 loc) · 4.63 KB
/
Monitor.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using System.Text;
using gma.System.Windows;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
namespace Herring
{
public delegate AppInfo GetAppDelegate(string path);
class Monitor
{
private UserActivityHook actHook;
private Dictionary<string, AppInfo> apps = new Dictionary<string, AppInfo>();
private DateTime begin;
private StringBuilder charsTyped;
private int keyPressCount;
private double mouseDistance;
private int mouseClicks;
private Point prevMouseLoc;
public Monitor()
{
Reset(DateTime.Now);
actHook = new UserActivityHook();
actHook.OnMouseActivity += MouseMoved;
actHook.KeyDown += KeyDown;
actHook.KeyPress += KeyPressed;
}
public void Start()
{
actHook.Start();
}
public void MouseMoved(object sender, MouseEventArgs e)
{
// Distance
if (prevMouseLoc.X != -1)
{
double dx = e.Location.X - prevMouseLoc.X;
double dy = e.Location.Y - prevMouseLoc.Y;
double d = Math.Sqrt(dx * dx + dy * dy);
mouseDistance += d;
}
prevMouseLoc = e.Location;
// Clicks
mouseClicks += e.Clicks / 2;
}
public void KeyDown(object sender, KeyEventArgs e)
{
keyPressCount++;
}
public void KeyPressed(object sender, KeyPressEventArgs e)
{
if (e.KeyChar < ' ') // \t, \r, \n etc.
{
if (e.KeyChar == '\b') // backspace
{
if (charsTyped.Length >= 1)
charsTyped.Remove(charsTyped.Length - 1, 1);
}
else
{
charsTyped.Append(' ');
}
}
else
{
charsTyped.Append(e.KeyChar);
}
}
// Takes "now" for strictly precise measurements
public void Reset(DateTime now)
{
begin = now;
charsTyped = new StringBuilder();
keyPressCount = 0;
prevMouseLoc = new Point(-1, -1);
mouseDistance = 0;
mouseClicks = 0;
}
public AppInfo GetApp(string path)
{
AppInfo appInfo;
if (apps.ContainsKey(path))
{
appInfo = apps[path];
}
else
{
Icon icon = ShellIcon.GetSmallIcon(path);
appInfo =
new AppInfo
{
Name = Path.GetFileName(path),
Path = path,
Icon = icon,
ColorBins = IconAnalyser.GetColors(icon)
};
apps.Add(path, appInfo);
}
return appInfo;
}
public ActivitySnapshot GetActivitySnapshot()
{
string path = SystemInfo.GetTopWindowPath();
string windowTitle, applicationTitle;
SystemInfo.GetTopWindowText(out windowTitle, out applicationTitle);
// Prepare AppInfo object
AppInfo appInfo = GetApp(path);
string doc = ""; ;
if (appInfo.Name == "chrome.exe")
{
doc = SystemInfo.GetChromeUrl();
if (doc == "(Incognito)")
{
applicationTitle = "(Incognito)";
windowTitle = "";
}
}
// Measure the time span since the previous snapshot
DateTime end = DateTime.Now;
TimeSpan length = end - begin;
// Create an activity data
ActivitySnapshot data =
new ActivitySnapshot
{
Time = begin,
App = appInfo,
ApplicationTitle = applicationTitle,
WindowTitle = windowTitle,
DocumentUrl = doc,
KeyboardIntensity = ActivitySnapshot.GetIntensity(keyPressCount, 6, length),
MouseIntensity = ActivitySnapshot.GetIntensity((int)mouseDistance, 1000, length)
};
Reset(end);
return data;
}
}
}