2016-04-17 13:24:33 +02:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-23 16:50:40 +02:00
|
|
|
var Launcher = require('../../comp/launcher');
|
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 = {
|
|
|
|
tab: '{tab}', enter: '{enter}', space: '{space}',
|
|
|
|
up: '{up}', down: '{down}', left: '{left}', right: '{right}', home: '{home}', end: '{end}', pgup: '{pgup}', pgdn: '{pgdn}',
|
|
|
|
ins: '{ins}', del: '{del}', bs: '{bs}', esc: '{esc}',
|
2016-04-18 22:46:54 +02:00
|
|
|
win: 0x5B, rwin: 0x5C,
|
2016-04-17 13:24:33 +02:00
|
|
|
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: '{add}', subtract: '{subtract}', multiply: '{multiply}', divide: '{divide}',
|
|
|
|
n0: '0', n1: '1', n2: '2', n3: '3', n4: '4',
|
|
|
|
n5: '5', n6: '6', n7: '7', n8: '8', n9: '9'
|
|
|
|
};
|
|
|
|
|
|
|
|
var ModMap = {
|
|
|
|
'^': '^',
|
|
|
|
'+': '+',
|
2016-04-18 22:46:54 +02:00
|
|
|
'%': '%',
|
2016-04-17 13:24:33 +02:00
|
|
|
'^^': '^'
|
|
|
|
};
|
|
|
|
|
2016-04-18 22:46:54 +02:00
|
|
|
var ModVk = {
|
|
|
|
'+': 0x10,
|
|
|
|
'^': 0x11,
|
|
|
|
'%': 0x12
|
|
|
|
};
|
|
|
|
|
2016-04-17 13:24:33 +02:00
|
|
|
var TextReplaceRegex = /[\(\)\{}\[\]\+\^%~]/g;
|
|
|
|
|
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-04-18 22:46:54 +02:00
|
|
|
text = this.addMod(this.escapeText(text.replace(TextReplaceRegex, function(match) { return '{' + match + '}'; })));
|
2016-04-19 19:39:17 +02:00
|
|
|
this.pendingScript.push('[System.Windows.Forms.SendKeys]::SendWait("' + 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') {
|
|
|
|
key = KeyMap[key];
|
|
|
|
if (!key) {
|
2016-04-19 21:19:08 +02:00
|
|
|
return this.callback('Bad key: ' + key);
|
2016-04-17 13:24:33 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-18 22:46:54 +02:00
|
|
|
if (typeof key === 'number') {
|
|
|
|
Object.keys(this.mod).forEach(function(mod) { this.pendingScript.push('[KwHelper]::Down(' + ModVk[mod] + ')'); }, this);
|
|
|
|
this.pendingScript.push('[KwHelper]::Press(' + key + ')');
|
|
|
|
Object.keys(this.mod).forEach(function(mod) { this.pendingScript.push('[KwHelper]::Up(' + ModVk[mod] + ')'); }, this);
|
|
|
|
} else {
|
|
|
|
var text = this.addMod(key);
|
2016-04-19 19:39:17 +02:00
|
|
|
this.pendingScript.push('[System.Windows.Forms.SendKeys]::SendWait("' + text + '")');
|
2016-04-18 22:46:54 +02:00
|
|
|
}
|
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-19 19:39:17 +02:00
|
|
|
this.pendingScript.push('[System.Threading.Thread]::Sleep(500)');
|
2016-04-18 22:46:54 +02:00
|
|
|
this.pendingScript.push('[System.Windows.Forms.Clipboard]::SetText("' + this.escapeText(text) + '")');
|
2016-04-19 19:39:17 +02:00
|
|
|
this.pendingScript.push('[System.Threading.Thread]::Sleep(500)');
|
2016-04-18 22:46:54 +02:00
|
|
|
this.pendingScript.push('[System.Windows.Forms.SendKeys]::SendWait("+{ins}")');
|
2016-04-19 19:39:17 +02:00
|
|
|
this.pendingScript.push('[System.Threading.Thread]::Sleep(500)');
|
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) {
|
|
|
|
this.pendingScript.push('[System.Threading.Thread]::Sleep(' + time + ')');
|
|
|
|
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');
|
|
|
|
};
|
|
|
|
|
|
|
|
AutoTypeEmitter.prototype.escapeText = function(text) {
|
2016-04-18 22:46:54 +02:00
|
|
|
return text.replace(/"/g, '`"').replace(/`/g, '``').replace(/\n/g, '``n');
|
|
|
|
};
|
|
|
|
|
2016-04-19 21:19:08 +02:00
|
|
|
AutoTypeEmitter.prototype.addMod = function(text) {
|
2016-04-17 13:24:33 +02:00
|
|
|
var keys = Object.keys(this.mod);
|
|
|
|
if (!keys.length || !text) {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
return keys.join('') + (text.length > 1 ? '(' + text + ')' : text);
|
|
|
|
};
|
|
|
|
|
2016-04-19 21:19:08 +02:00
|
|
|
AutoTypeEmitter.prototype.runScript = function(script) {
|
2016-04-19 19:39:17 +02:00
|
|
|
script =
|
|
|
|
'Add-Type @"\n' +
|
|
|
|
'using System;\n' +
|
|
|
|
'using System.Runtime.InteropServices;\n' +
|
|
|
|
'public static class KwHelper {\n' +
|
|
|
|
'[DllImport("user32.dll")]\n' +
|
|
|
|
'static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);\n' +
|
|
|
|
'public static void Down(byte code) { keybd_event(code, 0, 1, UIntPtr.Zero); }\n' +
|
|
|
|
'public static void Up(byte code) { keybd_event(code, 0, 3, UIntPtr.Zero); }\n' +
|
|
|
|
'public static void Press(byte code) { Down(code); Up(code); }\n' +
|
|
|
|
'}\n' +
|
|
|
|
'"@\n\n' +
|
|
|
|
'[Console]::InputEncoding = [System.Text.Encoding]::UTF8\n' +
|
|
|
|
'[Console]::OutputEncoding = [System.Text.Encoding]::UTF8\n' +
|
|
|
|
'[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")\n' +
|
|
|
|
script;
|
|
|
|
|
|
|
|
Launcher.spawn({
|
|
|
|
cmd: 'powershell',
|
|
|
|
args: ['-Command', '-'],
|
|
|
|
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;
|