-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
113 lines (96 loc) · 3.25 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
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
'use strict';
const ioHook = require('iohook');
const robot = require('robotjs');
const _ = require('lodash');
const Promise = require('bluebird');
// start ioHook
ioHook.start();
// ioHook.setDebug(true); // Uncomment this line for see all debug information from iohook
// Key constants
const DESKTOP_SHORTCUT = 'DESKTOP_SHORTCUT'
const EXECUTION_TIMEOUT = 500
const CTRL = 29;
const ALT = 56;
const F7 = 65;
// Astrokey Workflow
const myWorkflows = [{
_id: 'abcdefabcdef123123',
label: 'My New Workflow',
author: 'aeksco',
created_by: 'created_by_user_id',
public: true, // PUBLICLY VISIBLE BOOLEAN
version_major: 0, // WORKFLOW VERSION
version_minor: 1, // WORKFLOW VERSION MINOR
compatible_with: [], // DEVICE_TYPE_VERSIONS (?)
steps: [
// { id: 2, order: 2, icon: 'fa-play-circle-o', type: 'MACRO', label: 'Run Macro', value: [] },
// { id: 3, order: 3, type: 'KEY_UP', label: 'Release Key' },
{ id: 5, order: 5, icon: 'fa-paragraph', type: 'TEXT', label: 'Type', value: 'I love Alaina' },
{ id: 6, order: 6, icon: 'fa-cube', type: 'KEY', value: 'enter' },
{ id: 4, order: 4, icon: 'fa-clock-o', type: 'DELAY', label: 'Delay', value: 2000 },
{ id: 6, order: 6, icon: 'fa-cube', type: 'KEY', value: 'enter' },
{ id: 6, order: 6, icon: 'fa-cube', type: 'KEY', value: 'enter' },
{ id: 5, order: 5, icon: 'fa-paragraph', type: 'TEXT', label: 'Type', value: 'SO MUCHHHHH' }
],
triggers: [{
type: DESKTOP_SHORTCUT,
shortcut: [CTRL, F7]
}]
}]
function typeString (string) {
// console.log('Start typestring...')
return new Promise((resolve, reject) => {
// console.log('finish typestring.')
robot.typeString(string)
return resolve()
})
}
function keyTap (key) {
// console.log('Start keytap...')
return new Promise((resolve, reject) => {
// console.log('finish keytap.')
robot.keyTap(key)
return resolve()
})
}
function delay (timeout) {
// console.log('Start delay...')
return new Promise((resolve, reject) => {
setTimeout(() => {
// console.log('Delay finished.')
return resolve()
}, timeout)
})
}
// Setup keybindings
_.each(myWorkflows, (workflow) => {
_.each(workflow.triggers, (trigger) => {
if (trigger.type === DESKTOP_SHORTCUT) {
// Registers shortcut
ioHook.registerShortcut(trigger.shortcut, (keys) => {
console.log('Shortcut pressed with keys:', keys);
// console.log('EXECUTING WORKFLOW')
// Execute workflow
setTimeout(() => {
// NOTE - each step is asynchonous - this should be considered for things like delays
Promise.each(workflow.steps, (step) => {
if (step.type === 'TEXT') {
return typeString(step.value)
} else if (step.type === 'DELAY') {
return delay(step.value)
} else if (step.type === 'KEY') {
return keyTap(step.value)
}
}).then(() => {
// console.log('WORKFLOW COMPLETE')
})
}, EXECUTION_TIMEOUT)
});
}
})
})
// let shId = ioHook.registerShortcut([ALT, F7], (keys) => {
// console.log('This shortcut will be called once. Keys:', keys);
// ioHook.unregisterShortcut(shId);
// })
// console.log('Hook started. Try type something or move mouse');