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

112 lines
3.3 KiB
JavaScript
Raw Normal View History

2016-04-19 20:48:49 +02:00
'use strict';
2016-08-07 19:56:40 +02:00
const Launcher = require('../../comp/launcher');
const Locale = require('../../util/locale');
2016-04-19 20:48:49 +02:00
// https://cgit.freedesktop.org/xorg/proto/x11proto/plain/keysymdef.h
2016-08-07 19:56:40 +02:00
const KeyMap = {
2016-04-25 23:19:40 +02:00
tab: 'Tab', enter: 'KP_Enter', space: 'KP_Space',
up: 'Up', down: 'Down', left: 'Left', right: 'Right', home: 'Home', end: 'End', pgup: 'Page_Up', pgdn: 'Page_Down',
ins: 'Insert', del: 'Delete', bs: 'BackSpace', esc: 'Escape',
win: 'Meta_L', rwin: 'Meta_R',
f1: 'F1', f2: 'F2', f3: 'F3', f4: 'F4', f5: 'F5', f6: 'F6', f7: 'F7', f8: 'F8', f9: 'F9',
f10: 'F10', f11: 'F11', f12: 'F12', f13: 'F13', f14: 'F14', f15: 'F15', f16: 'F16',
add: 'KP_Add', subtract: 'KP_Subtract', multiply: 'KP_Multiply', divide: 'KP_Divide',
n0: 'KP_0', n1: 'KP_1', n2: 'KP_2', n3: 'KP_3', n4: 'KP_4',
n5: 'KP_5', n6: 'KP_6', n7: 'KP_7', n8: 'KP_8', n9: 'KP_9'
2016-04-19 20:48:49 +02:00
};
2016-08-07 19:56:40 +02:00
const ModMap = {
2016-04-19 20:48:49 +02:00
'^': 'ctrl',
'+': 'shift',
'%': 'alt',
'^^': 'ctrl'
};
2016-04-19 21:19:08 +02:00
var AutoTypeEmitter = function(callback) {
this.callback = callback;
2016-04-19 20:48:49 +02:00
this.mod = {};
this.pendingScript = [];
};
2016-04-19 21:19:08 +02:00
AutoTypeEmitter.prototype.setMod = function(mod, enabled) {
2016-04-19 20:48:49 +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-09-13 07:28:41 +02:00
this.pendingScript.push('keyup ctrl alt shift t');
2016-07-17 13:30:38 +02:00
Object.keys(this.mod).forEach(mod => {
this.pendingScript.push('keydown ' + ModMap[mod]);
2016-04-25 23:19:40 +02:00
});
2016-07-30 21:12:49 +02:00
text.split('').map(char => {
this.pendingScript.push('key U' + char.charCodeAt(0).toString(16));
2016-04-25 23:19:40 +02:00
});
2016-07-30 21:12:49 +02:00
Object.keys(this.mod).forEach(mod => {
this.pendingScript.push('keyup ' + ModMap[mod]);
});
this.waitComplete();
2016-04-19 20:48:49 +02:00
};
2016-04-19 21:19:08 +02:00
AutoTypeEmitter.prototype.key = function(key) {
2016-04-19 20:48:49 +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-19 20:48:49 +02:00
}
key = KeyMap[key].toString(16);
}
2016-04-25 23:19:40 +02:00
this.pendingScript.push('key --clearmodifiers ' + this.modString() + key);
2016-04-19 21:19:08 +02:00
this.callback();
2016-04-19 20:48:49 +02:00
};
2016-04-19 21:19:08 +02:00
AutoTypeEmitter.prototype.copyPaste = function(text) {
2016-04-25 23:19:40 +02:00
this.pendingScript.push('sleep 0.5');
2016-04-19 21:21:32 +02:00
Launcher.setClipboardText(text);
2016-04-25 23:19:40 +02:00
this.pendingScript.push('key --clearmodifiers shift+Insert');
this.pendingScript.push('sleep 0.5');
2016-04-19 21:21:32 +02:00
this.waitComplete();
2016-04-19 20:48:49 +02:00
};
2016-07-14 20:56:48 +02:00
AutoTypeEmitter.prototype.wait = function(time) {
this.pendingScript.push('sleep ' + (time / 1000));
this.callback();
};
2016-04-25 23:19:40 +02:00
AutoTypeEmitter.prototype.waitComplete = function(callback) {
2016-04-19 20:48:49 +02:00
if (this.pendingScript.length) {
var script = this.pendingScript.join(' ');
this.pendingScript.length = 0;
2016-04-25 23:19:40 +02:00
this.runScript(script, callback);
2016-04-19 20:48:49 +02:00
} else {
2016-04-19 21:19:08 +02:00
this.callback();
2016-04-19 20:48:49 +02:00
}
};
2016-04-19 21:19:08 +02:00
AutoTypeEmitter.prototype.modString = function() {
2016-04-19 20:48:49 +02:00
var mod = '';
2016-07-17 13:30:38 +02:00
Object.keys(this.mod).forEach(key => {
2016-04-19 20:48:49 +02:00
mod += key + '+';
});
return mod;
};
2016-04-25 23:19:40 +02:00
AutoTypeEmitter.prototype.runScript = function(script, callback) {
2016-04-19 20:48:49 +02:00
Launcher.spawn({
cmd: 'xdotool',
2016-04-25 23:19:40 +02:00
args: ['-'],
2016-04-19 20:48:49 +02:00
data: script,
2016-08-07 19:56:40 +02:00
complete: (err, stdout, code) => {
if (err && err.code === 'ENOENT') {
err = Locale.autoTypeErrorNotInstalled.replace('{}', 'xdotool');
}
let cb = callback || this.callback;
cb(err, stdout, code);
}
2016-04-19 20:48:49 +02:00
});
};
2016-04-19 21:19:08 +02:00
module.exports = AutoTypeEmitter;