forked from Dhanushsai0407/Vi-Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionTracker.ts
More file actions
55 lines (46 loc) · 1.27 KB
/
SessionTracker.ts
File metadata and controls
55 lines (46 loc) · 1.27 KB
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
export interface KeyStroke {
key: string;
timestamp: number;
type: 'insert' | 'delete' | 'navigation' | 'paste' | 'other';
pauseBefore: number;
}
export class SessionTracker {
private keystrokes: KeyStroke[] = [];
private lastKeyTime: number = 0;
private sessionStartTime: number = 0;
startSession() {
this.keystrokes = [];
this.sessionStartTime = Date.now();
this.lastKeyTime = this.sessionStartTime;
}
recordKey(key: string, isPaste: boolean = false) {
if (!this.sessionStartTime) return;
const now = Date.now();
const pauseBefore = this.lastKeyTime ? now - this.lastKeyTime : 0;
let type: KeyStroke['type'] = 'insert';
if (isPaste) {
type = 'paste';
} else if (key === 'Backspace' || key === 'Delete') {
type = 'delete';
} else if (key.startsWith('Arrow') || key === 'Home' || key === 'End') {
type = 'navigation';
} else if (key.length > 1 && key !== 'Enter' && key !== 'Space') {
type = 'other';
}
this.keystrokes.push({
key,
timestamp: now,
type,
pauseBefore
});
this.lastKeyTime = now;
}
getLog(): KeyStroke[] {
return this.keystrokes;
}
clear() {
this.keystrokes = [];
this.sessionStartTime = 0;
this.lastKeyTime = 0;
}
}