-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathActivityControl.cs
61 lines (54 loc) · 1.96 KB
/
ActivityControl.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Gma.System.MouseKeyHook;
using System.Windows.Forms;
namespace pctesting
{
class ActivityControl
{
private IKeyboardMouseEvents m_GlobalHook;
private DateTime StartWorkTime;
private DateTime LastInput;
private TimeSpan PassiveTime;
string user;
string comp;
public ActivityControl(string user, string comp)
{
this.user=user;
this.comp=comp;
}
public void Subscribe()
{
StartWorkTime = DateTime.Now;
LastInput = StartWorkTime;
// Note: for the application hook, use the Hook.AppEvents() instead
m_GlobalHook = Hook.GlobalEvents();
m_GlobalHook.MouseDownExt += GlobalHookMouseDownExt;
m_GlobalHook.KeyPress += GlobalHookKeyPress;
}
private void GlobalHookKeyPress(object sender, KeyPressEventArgs e)
{
PassiveTime += (DateTime.Now - LastInput);
LastInput = DateTime.Now;
}
private void GlobalHookMouseDownExt(object sender, MouseEventExtArgs e)
{
PassiveTime += (DateTime.Now - LastInput);
LastInput = DateTime.Now;
// uncommenting the following line will suppress the middle mouse button click
// if (e.Buttons == MouseButtons.Middle) { e.Handled = true; }
}
public void Unsubscribe()
{
DBService.DataServiceClient client = new DBService.DataServiceClient();
m_GlobalHook.MouseDownExt -= GlobalHookMouseDownExt;
m_GlobalHook.KeyPress -= GlobalHookKeyPress;
//It is recommened to dispose it
m_GlobalHook.Dispose();
client.SaveActivityToDB((DateTime.Now - StartWorkTime), (DateTime.Now - StartWorkTime)-PassiveTime,PassiveTime,comp,user);
}
}
}