-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
65 lines (56 loc) · 1.54 KB
/
index.js
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
//Move the mouse across the screen as a sine wave.
var robot = require("robotjs");
var async = require("async");
DELAY = 5
var test_console = {
log: function (string) {
if (process.env.NODE_ENV === 'test'){
console.log(string);
}
}}
var executeKeyDown = function (command, callback) {
test_console.log('holding down '+command)
robot.keyToggle(command,'down');
setTimeout(function() {
callback();
}, DELAY);
}
var executeKeyUp = function (command, callback) {
test_console.log('releasing '+command)
robot.keyToggle(command,'up');
callback();
}
var processCommand = function (command,callback) {
test_console.log('pressing ' + command);
if (Array.isArray(command)){
var holds = command.slice(0,-1); //cuts off all but the last key
async.map(holds, executeKeyDown, function(){
test_console.log('tapping ' + command.slice(-1)[0] );
robot.keyTap(command.slice(-1)[0] );
async.map(holds,executeKeyUp,function () {
callback();
});
});
} else {
robot.keyTap(command);
setTimeout(function() {
callback();
}, DELAY);
}
}
var processComplexCommand = function(commands, callback, OPTdelay){
if(typeof OPTdelay !== "undefined") {
test_console.log('setting delay to '+ OPTdelay )
DELAY = OPTdelay;
}
if (Array.isArray(commands) && Array.isArray(commands[0])){
async.mapSeries(commands, processCommand,function () {
callback();
})
} else {
processCommand(commands,function () {
callback();
});
}
}
module.exports = processComplexCommand