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

87 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-04-19 20:48:49 +02:00
'use strict';
2016-04-23 16:50:40 +02:00
var Launcher = require('../../comp/launcher');
2016-04-19 20:48:49 +02:00
// https://cgit.freedesktop.org/xorg/proto/x11proto/plain/keysymdef.h
var KeyMap = {
tab: '0xff89', enter: '0xff8d', space: '0xff80',
up: '0xff97', down: '0xff99', left: '0xff96', right: '0xff98', home: '0xff95', end: '0xff9c', pgup: '0xff9a', pgdn: '0xff9b',
ins: '0xff9e', del: '0xffff', bs: '0xff08', esc: '0xff1b',
win: '0xffe7', rwin: '0xffe8',
f1: '0xffbe', f2: '0xffbf', f3: '0xffc0', f4: '0xffc1', f5: '0xffc2', f6: '0xffc3', f7: '0xffc4', f8: '0xffc5', f9: '0xffc6',
f10: '0xffc7', f11: '0xffc8', f12: '0xffc9', f13: '0xffca', f14: '0xffcb', f15: '0xffcc', f16: '0xffcd',
add: '0xffab', subtract: '0xffad', multiply: '0xffaa', divide: '0xffaf',
n0: '0xffb0', n1: '0xffb1', n2: '0xffb2', n3: '0xffb3', n4: '0xffb4',
n5: '0xffb5', n6: '0xffb6', n7: '0xffb7', n8: '0xffb8', n9: '0xffb9'
};
var ModMap = {
'^': '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-04-19 20:48:49 +02:00
this.pendingScript.push('text ' + this.modString() + text);
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.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);
}
this.pendingScript.push('key ' + 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-19 21:21:32 +02:00
Launcher.setClipboardText(text);
this.waitComplete();
2016-04-19 20:48:49 +02:00
};
2016-04-19 21:19:08 +02:00
AutoTypeEmitter.prototype.waitComplete = function() {
2016-04-19 20:48:49 +02:00
if (this.pendingScript.length) {
var script = this.pendingScript.join(' ');
this.pendingScript.length = 0;
2016-04-19 21:19:08 +02:00
this.runScript(script);
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 = '';
Object.keys(this.mod).forEach(function (key) {
mod += key + '+';
});
return mod;
};
2016-04-19 21:19:08 +02:00
AutoTypeEmitter.prototype.runScript = function(script) {
2016-04-19 20:48:49 +02:00
Launcher.spawn({
cmd: 'xdotool',
data: script,
2016-04-19 21:19:08 +02:00
complete: this.callback
2016-04-19 20:48:49 +02:00
});
};
2016-04-19 21:19:08 +02:00
module.exports = AutoTypeEmitter;