keeweb/app/scripts/auto-type/emitter/auto-type-emitter-darwin.js

92 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-07-24 09:53:34 +02:00
const Launcher = require('../../comp/launcher');
const AutoTypeNativeHelper = require('../helper/auto-type-native-helper');
2016-04-09 20:10:11 +02:00
2016-04-22 18:39:35 +02:00
// http://eastmanreference.com/complete-list-of-applescript-key-codes/
2017-01-31 07:50:28 +01:00
const KeyMap = {
2016-04-09 20:10:11 +02:00
tab: 48, enter: 36, space: 49,
up: 126, down: 125, left: 123, right: 124, home: 115, end: 119, pgup: 116, pgdn: 121,
ins: 114, del: 117, bs: 51, esc: 53,
win: 55, rwin: 55,
f1: 122, f2: 120, f3: 99, f4: 118, f5: 96, f6: 97, f7: 98, f8: 100, f9: 101,
f10: 109, f11: 103, f12: 111, f13: 105, f14: 107, f15: 113, f16: 106,
add: 69, subtract: 78, multiply: 67, divide: 75,
n0: 82, n1: 83, n2: 84, n3: 85, n4: 86,
n5: 87, n6: 88, n7: 89, n8: 91, n9: 92
};
2017-01-31 07:50:28 +01:00
const ModMap = {
2016-07-24 09:53:34 +02:00
'^': '@',
'+': '+',
'%': '%',
'^^': '^'
2016-04-09 20:10:11 +02:00
};
2017-01-31 07:50:28 +01:00
const AutoTypeEmitter = function(callback) {
2016-04-19 21:19:08 +02:00
this.callback = callback;
2016-04-09 20:10:11 +02:00
this.mod = {};
2016-04-10 08:32:18 +02:00
this.pendingScript = [];
2016-04-09 17:07:53 +02:00
};
2016-04-19 21:19:08 +02:00
AutoTypeEmitter.prototype.setMod = function(mod, enabled) {
2016-04-09 20:10:11 +02:00
if (enabled) {
2016-07-24 09:53:34 +02:00
this.mod[ModMap[mod]] = true;
2016-04-09 20:10:11 +02:00
} else {
2016-07-24 09:53:34 +02:00
delete this.mod[ModMap[mod]];
2016-04-09 20:10:11 +02:00
}
2016-04-09 17:07:53 +02:00
};
2016-04-19 21:19:08 +02:00
AutoTypeEmitter.prototype.text = function(text) {
2016-08-07 17:24:25 +02:00
this.pendingScript.push('text ' + this.modString() + ' ' + text);
2016-04-19 21:19:08 +02:00
this.callback();
2016-04-09 17:07:53 +02:00
};
2016-04-19 21:19:08 +02:00
AutoTypeEmitter.prototype.key = function(key) {
2016-04-09 20:10:11 +02:00
if (typeof key !== 'number') {
if (!KeyMap[key]) {
2016-04-19 21:19:08 +02:00
return this.callback('Bad key: ' + key);
2016-04-09 20:10:11 +02:00
}
key = KeyMap[key];
}
2016-07-24 09:53:34 +02:00
this.pendingScript.push('key ' + this.modString() + key);
2016-04-19 21:19:08 +02:00
this.callback();
2016-04-10 08:32:18 +02:00
};
2016-04-19 21:19:08 +02:00
AutoTypeEmitter.prototype.copyPaste = function(text) {
2016-07-24 09:53:34 +02:00
this.pendingScript.push('copypaste ' + text);
2016-04-19 21:19:08 +02:00
this.callback();
2016-04-09 17:07:53 +02:00
};
2016-04-19 21:19:08 +02:00
AutoTypeEmitter.prototype.wait = function(time) {
2016-07-24 09:53:34 +02:00
this.pendingScript.push('wait ' + time);
2016-04-19 21:19:08 +02:00
this.callback();
};
AutoTypeEmitter.prototype.waitComplete = function() {
2016-04-10 08:32:18 +02:00
if (this.pendingScript.length) {
2017-01-31 07:50:28 +01:00
const script = this.pendingScript.join('\n');
2016-04-10 08:32:18 +02:00
this.pendingScript.length = 0;
2016-04-19 21:19:08 +02:00
this.runScript(script);
2016-04-10 08:32:18 +02:00
} else {
2016-04-19 21:19:08 +02:00
this.callback();
2016-04-10 08:32:18 +02:00
}
2016-04-09 17:07:53 +02:00
};
2016-04-19 21:19:08 +02:00
AutoTypeEmitter.prototype.setDelay = function(delay) {
this.delay = delay || 0;
this.callback('Not implemented');
};
AutoTypeEmitter.prototype.modString = function() {
2016-07-24 09:53:34 +02:00
return Object.keys(this.mod).join('');
2016-04-09 20:10:11 +02:00
};
2016-04-19 21:19:08 +02:00
AutoTypeEmitter.prototype.runScript = function(script) {
2016-04-19 19:39:17 +02:00
Launcher.spawn({
2016-07-24 09:53:34 +02:00
cmd: AutoTypeNativeHelper.getHelperPath(),
2016-04-19 19:39:17 +02:00
data: script,
2016-04-19 21:19:08 +02:00
complete: this.callback
2016-04-19 19:39:17 +02:00
});
2016-04-09 20:10:11 +02:00
};
2016-04-19 21:19:08 +02:00
module.exports = AutoTypeEmitter;