-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinput.js
More file actions
144 lines (98 loc) · 3.32 KB
/
input.js
File metadata and controls
144 lines (98 loc) · 3.32 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
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
// this bot is not moving cursor continuously for performace's sake
// this bot used robot.js and iohook.jsw
'use strict';
// add required modules
var hook= require('iohook');
var robot = require('robotjs');
var events = require('events');
var eventEmitter = new events.EventEmitter();
var mouse, currenttime, duration, data, interval;
var dragFlag = true;
var fs = require('fs');
var writeStream = fs.createWriteStream('bot.txt');
// initialize a 4 row array to record coordinates, key character, interval time
// x-coordinate, y-coordinate, time from last press, button character thats being pressed
var captureMotion = [];
var Motion = function(x, y, interval, rawcode) {
this.x = x;
this.y = y;
this.interval = interval;
this.rawcode = rawcode;
}
//----------------------------------------------------------------------//
console.log('recording starts');
console.log('press tab to stop');
function getTime(){
var date= new Date();
return date.getTime();
};
var baseTime = getTime();
// binding event keydown and listener
hook.on("keydown", (event)=> {
mouse= robot.getMousePos();
currenttime = getTime();
interval= currenttime - baseTime;
// prevent zero time
if(interval == 0 ) interval =1;
captureMotion.push(new Motion(mouse.x, mouse.y, interval, event.rawcode));
baseTime = getTime();
dragFlag = true;
// press tab to stop
if(event.rawcode == 48) eventEmitter.emit('stopListening');
});
hook.addListener("mousedown", (event)=> {
mouse= robot.getMousePos();
currenttime = getTime();
interval= currenttime - baseTime;
if(interval == 0 ) interval =1;
// set the keycode of mouseclick as 127
captureMotion.push(new Motion(mouse.x, mouse.y, interval, 127));
dragFlag = true;
baseTime = getTime();
});
hook.addListener('mousedrag', (event)=> {
// set the keycode of mousedrag as 128
if(dragFlag == true) {
currenttime = getTime();
interval= currenttime - baseTime;
if(interval == 0 ) interval =1;
/*
* change this to mouseclick temporily,
* since the mouseclick events often is listened wrongly as mousedrag
*/
captureMotion.push(new Motion(event.x, event.y, interval ,127));
}
dragFlag = false;
});
// sed to handle the mousedrag release
hook.addListener('mouseup', event=> {
mouse= robot.getMousePos();
if( dragFlag == false){
// prevent zero time
if(interval == 0 ) interval =1;
captureMotion.push(new Motion(event.x, event.y, 100, 128));
baseTime = getTime();
}
})
// start listening
hook.start();
//------------------------------------------------------------//
eventEmitter.once('stopListening',()=>{
// stop listening to events
eventEmitter.removeAllListeners('keydown');
eventEmitter.removeAllListeners('mousedown');
eventEmitter.removeAllListeners('mouseup');
eventEmitter.removeAllListeners('mousedrag');
var buffer = new Buffer.from(JSON.stringify(captureMotion));
writeStream.write(buffer, 'utf8');
writeStream.end();
writeStream.on('finish', function(){
console.log('writing completed');
hook.stop();
process.exit(0);
});
writeStream.on('error', function(err){
hook.stop();
console.log(err.stack);
});
});