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

96 lines
2.5 KiB
JavaScript
Raw Normal View History

2016-04-17 13:24:33 +02:00
'use strict';
2016-04-24 22:03:52 +02:00
var Launcher = require('../../comp/launcher'),
2016-07-24 09:53:34 +02:00
AutoTypeNativeHelper = require('../helper/auto-type-native-helper');
2016-04-17 13:24:33 +02:00
2016-04-22 18:39:35 +02:00
// https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx
2016-04-17 13:24:33 +02:00
var KeyMap = {
2016-08-08 19:55:30 +02:00
tab: 0x09, enter: 0x0D, space: 0x20,
up: 0x26, down: 0x28, left: 0x25, right: 0x27, home: 0x24, end: 0x23, pgup: 0x21, pgdn: 0x22,
ins: 0x2D, del: 0x2E, bs: 0x08, esc: 0x1B,
2016-04-18 22:46:54 +02:00
win: 0x5B, rwin: 0x5C,
2016-08-08 19:55:30 +02:00
f1: 0x70, f2: 0x71, f3: 0x72, f4: 0x73, f5: 0x74, f6: 0x75, f7: 0x76, f8: 0x77, f9: 0x78,
f10: 0x79, f11: 0x7A, f12: 0x7B, f13: 0x7C, f14: 0x7D, f15: 0x7E, f16: 0x7F,
add: 0x6B, subtract: 0x6D, multiply: 0x6A, divide: 0x6F,
n0: 0x30, n1: 0x31, n2: 0x32, n3: 0x33, n4: 0x34,
n5: 0x35, n6: 0x36, n7: 0x37, n8: 0x38, n9: 0x39
2016-04-17 13:24:33 +02:00
};
var ModMap = {
'^': '^',
'+': '+',
2016-04-18 22:46:54 +02:00
'%': '%',
2016-04-17 13:24:33 +02:00
'^^': '^'
};
2016-04-19 21:19:08 +02:00
var AutoTypeEmitter = function(callback) {
this.callback = callback;
2016-04-17 13:24:33 +02:00
this.mod = {};
this.pendingScript = [];
};
2016-04-19 21:19:08 +02:00
AutoTypeEmitter.prototype.setMod = function(mod, enabled) {
2016-04-17 13:24:33 +02:00
if (enabled) {
this.mod[ModMap[mod]] = true;
} else {
delete this.mod[ModMap[mod]];
}
};
2016-04-19 21:19:08 +02:00
AutoTypeEmitter.prototype.text = function(text) {
2016-08-08 19:55:30 +02:00
if (text) {
this.pendingScript.push('text ' + this.modStr() + ' ' + text);
}
2016-04-19 21:19:08 +02:00
this.callback();
2016-04-17 13:24:33 +02:00
};
2016-04-19 21:19:08 +02:00
AutoTypeEmitter.prototype.key = function(key) {
2016-04-17 13:24:33 +02:00
if (typeof key !== 'number') {
2016-07-24 09:53:34 +02:00
if (!KeyMap[key]) {
2016-04-19 21:19:08 +02:00
return this.callback('Bad key: ' + key);
2016-04-17 13:24:33 +02:00
}
2016-07-24 09:53:34 +02:00
key = KeyMap[key];
2016-04-17 13:24:33 +02:00
}
2016-08-08 19:55:30 +02:00
this.pendingScript.push('key ' + this.modStr() + key);
2016-04-19 21:19:08 +02:00
this.callback();
2016-04-17 13:24:33 +02:00
};
2016-04-19 21:19:08 +02:00
AutoTypeEmitter.prototype.copyPaste = function(text) {
2016-04-24 14:08:46 +02:00
this.pendingScript.push('copypaste ' + text);
2016-04-19 21:19:08 +02:00
this.callback();
2016-04-17 13:24:33 +02:00
};
2016-04-19 21:19:08 +02:00
AutoTypeEmitter.prototype.wait = function(time) {
2016-04-24 14:08:46 +02:00
this.pendingScript.push('wait ' + time);
2016-04-19 21:19:08 +02:00
this.callback();
};
AutoTypeEmitter.prototype.waitComplete = function() {
2016-04-17 13:24:33 +02:00
if (this.pendingScript.length) {
2016-04-18 22:46:54 +02:00
var script = this.pendingScript.join('\n');
2016-04-17 13:24:33 +02:00
this.pendingScript.length = 0;
2016-04-19 21:19:08 +02:00
this.runScript(script);
2016-04-17 13:24:33 +02:00
} else {
2016-04-19 21:19:08 +02:00
this.callback();
2016-04-17 13:24:33 +02:00
}
};
2016-04-19 21:19:08 +02:00
AutoTypeEmitter.prototype.setDelay = function(delay) {
this.delay = delay || 0;
this.callback('Not implemented');
};
2016-08-08 19:55:30 +02:00
AutoTypeEmitter.prototype.modStr = function() {
return Object.keys(this.mod).join('');
2016-04-17 13:24:33 +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-17 13:24:33 +02:00
};
2016-04-19 21:19:08 +02:00
module.exports = AutoTypeEmitter;