node-key-sender
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -7,2 +7,16 @@ var exec = require('child_process').exec; | ||
var batch = []; | ||
var options = { | ||
"startDelayMillisec": null, | ||
"caseCorrection": null, | ||
"globalDelayPressMillisec": null, | ||
"globalDelayBetweenMillisec": null, | ||
"extra": null | ||
}; | ||
module.BATCH_EVENT_KEY_PRESS = 1; | ||
module.BATCH_EVENT_KEY_UP = 2; | ||
module.BATCH_EVENT_KEY_DOWN = 3; | ||
/** | ||
@@ -102,3 +116,3 @@ * Keyboard layout mapping. This mapping table can be set with setKeyboardLayout(). | ||
var command = 'java -jar ' + jarPath + ' ' + arrParams.join(' '); | ||
var command = 'java -jar ' + jarPath + ' ' + arrParams.join(' ') + module.getCommandLineOptions(); | ||
console.log('Sending: ' + command); | ||
@@ -116,2 +130,32 @@ | ||
module.getCommandLineOptions = function() { | ||
var arguments = ''; | ||
if (typeof options.startDelayMillisec !== 'undefined' && options.startDelayMillisec != null) { | ||
arguments = arguments + ' -sd ' + options.startDelayMillisec; | ||
} | ||
if (typeof options.caseCorrection !== 'undefined' && options.caseCorrection != null) { | ||
arguments = arguments + ' -c ' + (options.caseCorrection ? '1' : '0'); | ||
} | ||
if (typeof options.globalDelayPressMillisec !== 'undefined' && options.globalDelayPressMillisec != null) { | ||
arguments = arguments + ' -pd ' + options.globalDelayPressMillisec; | ||
} | ||
if (typeof options.globalDelayBetweenMillisec !== 'undefined' && options.globalDelayBetweenMillisec != null) { | ||
arguments = arguments + ' -d ' + options.globalDelayBetweenMillisec; | ||
} | ||
if (typeof options.extra !== 'undefined' && options.extra != null) { | ||
arguments = arguments + ' ' + options.extra; | ||
} | ||
return arguments; | ||
}; | ||
module.cleanKeyboardLayout = function() { | ||
keyboardLayout = {}; | ||
}; | ||
module.setKeyboardLayout = function(newKeyMap) { | ||
@@ -121,4 +165,4 @@ keyboardLayout = newKeyMap; | ||
module.aggregateKeyboardLayout = function(arrKeyMap) { | ||
keyboardLayout = Object.assign(keyboardLayout, arrKeyMap) | ||
module.aggregateKeyboardLayout = function(objKeyMap) { | ||
keyboardLayout = Object.assign(keyboardLayout, objKeyMap) | ||
}; | ||
@@ -130,3 +174,122 @@ | ||
module.startBatch = function() { | ||
batch = []; | ||
return module; | ||
}; | ||
module.batchTypeKey = function(keyCode, waitMillisec, batchEvent) { | ||
if (typeof waitMillisec == 'undefined') { | ||
waitMillisec = 0; | ||
} | ||
if (typeof batchEvent == 'undefined') { | ||
batchEvent = module.BATCH_EVENT_KEY_PRESS; | ||
} | ||
var param = { | ||
"combination": null, | ||
"keyCode": keyCode, | ||
"wait": waitMillisec, | ||
"event": batchEvent | ||
}; | ||
batch.push(param); | ||
return this; | ||
}; | ||
module.batchTypeCombination = function(arrKeys, waitMillisec, batchEvent) { | ||
if (typeof waitMillisec == 'undefined') { | ||
waitMillisec = 0; | ||
} | ||
if (typeof batchEvent == 'undefined') { | ||
batchEvent = module.BATCH_EVENT_KEY_PRESS; | ||
} | ||
var param = { | ||
"combination": arrKeys, | ||
"keyCode": null, | ||
"wait": waitMillisec, | ||
"event": batchEvent | ||
}; | ||
batch.push(param); | ||
return this; | ||
}; | ||
module.batchTypeKeys = function(arrKeyCodes) { | ||
for (var i = 0; i < arrKeyCodes.length; i++) { | ||
var param = { | ||
"combination": null, | ||
"keyCode": arrKeyCodes[i], | ||
"wait": 0, | ||
"event": module.BATCH_EVENT_KEY_PRESS | ||
}; | ||
batch.push(param); | ||
} | ||
return this; | ||
}; | ||
module.batchTypeText = function(text) { | ||
var arrKeyCodes = []; | ||
for (var i = 0, len = text.length; i < len; i++) { | ||
var currentKey = text[i]; | ||
var keyCode = module.getKeyCode(currentKey); | ||
arrKeyCodes.push(keyCode); | ||
} | ||
module.batchTypeKeys(arrKeyCodes); | ||
return this; | ||
}; | ||
module.sendBatch = function() { | ||
var arrArguments = []; | ||
for (var i = 0; i < batch.length; i++) { | ||
var param = batch[i]; | ||
var isCombination = param.combination != null; | ||
var argument = ''; | ||
if (isCombination) { | ||
argument = param.combination.join('-'); | ||
} else { | ||
argument = param.keyCode; | ||
} | ||
if (param.wait > 0) { | ||
argument = argument + '.w' + param.wait; | ||
} | ||
if (param.event == module.BATCH_EVENT_KEY_UP) { | ||
argument = argument + '.up'; | ||
} | ||
if (param.event == module.BATCH_EVENT_KEY_DOWN) { | ||
argument = argument + '.down'; | ||
} | ||
arrArguments.push(argument); | ||
} | ||
return module.execute(arrArguments); | ||
}; | ||
module.setOption = function(optionName, optionValue) { | ||
options[optionName] = optionValue; | ||
return module; | ||
}; | ||
module.getOptions = function() { | ||
return options; | ||
}; | ||
return module; | ||
}; | ||
}(); |
{ | ||
"name": "node-key-sender", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Lib to send keystrokes to the operation system.", | ||
@@ -5,0 +5,0 @@ "main": "key-sender.js", |
166
README.md
@@ -16,2 +16,3 @@ # node-key-sender | ||
- Possibility to map key codes; | ||
- Case correction for text | ||
- Multi platform (it will work in all operation systems that Java can run); | ||
@@ -46,3 +47,3 @@ - It will send the key to the current focused application in the operational system. | ||
var ks = require('node-key-sender')(); | ||
var ks = require('node-key-sender'); | ||
ks.sendKey('a'); | ||
@@ -52,3 +53,3 @@ | ||
var ks = require('node-key-sender')(); | ||
var ks = require('node-key-sender'); | ||
ks.sendKeys(['a', 'b', 'c']); | ||
@@ -58,3 +59,3 @@ | ||
var ks = require('node-key-sender')(); | ||
var ks = require('node-key-sender'); | ||
ks.sendCombination(['control', 'shift', 'v']); | ||
@@ -119,7 +120,164 @@ | ||
var ks = require('node-key-sender')(); | ||
var ks = require('node-key-sender'); | ||
ks.aggregateKeyboardLayout(accentsMap); | ||
ks.sendText("Héllõ Wíth Áçcents"); | ||
Sending batch: | ||
var ks = require('node-key-sender'); | ||
ks.startBatch() | ||
.batchTypeKey('N') | ||
.batchTypeKey('o') | ||
.batchTypeKey('d') | ||
.batchTypeKey('e') | ||
.batchTypeKeys(['N', 'o', 'd', 'e']) | ||
.batchTypeText('Node') | ||
.batchTypeKey('N', 1000) | ||
.batchTypeKey('o', 1000) | ||
.batchTypeKey('d', 1000) | ||
.batchTypeKey('e', 1000) | ||
.sendBatch(); | ||
Setting global press delay (in milliseconds): | ||
ks.setOption('globalDelayPressMillisec', 1000); | ||
Setting global delay between keys (in milliseconds): | ||
ks.setOption('globalDelayBetweenMillisec', 1000); | ||
Setting start delay (in milliseconds): | ||
ks.setOption('startDelayMillisec', 1000); | ||
Turning off case correction: | ||
ks.setOption('caseCorrection', false); | ||
# List of methods | ||
## Economic methods | ||
Use this methods if you want to send just a small amount of keys. Note that the jar file is called each time one of these methods are called: | ||
**sendKey(keyCode: string): Promise** | ||
Sends one key code. | ||
**sendKeys(arrKeyCodes: array): Promise** | ||
Sends multiple key codes. | ||
**sendLetter(letter: char): Promise** | ||
Sends a letter. A letter will be automatically converted to key code according to the keyboard layout configuration. You may set this configuration with `cleanKeyboardLayout`, `setKeyboardLayout` or `aggregateKeyboardLayout`. | ||
**sendText(text: string): Promise** | ||
Sends a text. A text will have its letters automatically converted to key codes according to the keyboard layout configuration. You may set this configuration with `cleanKeyboardLayout`, `setKeyboardLayout` or `aggregateKeyboardLayout`. | ||
**sendCombination(arrKeyCodes: array): Promise** | ||
Sends multiple key codes pressed together. | ||
## Batch methods | ||
Use this methods to send a large amount of keys. The jar file is executed each time you call `sendBatch`. You should start with `startBatch` and end with `sendBatch`: | ||
**startBatch()** | ||
Starts a batch. | ||
**sendBatch(): Promise** | ||
Sends the batch. | ||
**batchTypeKey(keyCode: string, waitMillisec: int, batchEvent: int)** | ||
Sends a key code. You may pass a delay it will wait until it presses the next key and also the type of event. Type of event is `ks.BATCH_EVENT_KEY_PRESS`, `ks.BATCH_EVENT_KEY_UP` and `ks.BATCH_EVENT_KEY_DOWN`. | ||
**batchTypeKeys(arrKeyCodes: array)** | ||
Sends a list of key codes, pressed one after the other. | ||
**batchTypeCombination(arrKeys: array, waitMillisec: int, batchEvent: int)** | ||
Sends a combination - list of key codes that will be pressed together. You may pass a delay it will wait until it presses the next key and also the type of event. Type of event is `ks.BATCH_EVENT_KEY_PRESS`, `ks.BATCH_EVENT_KEY_UP` and `ks.BATCH_EVENT_KEY_DOWN`. | ||
**batchTypeText(text: string)** | ||
Sends a text. A text will have its letters automatically converted to key codes according to the keyboard layout configuration. You may set this configuration with `cleanKeyboardLayout`, `setKeyboardLayout` or `aggregateKeyboardLayout`. | ||
## Keyboard layout methods | ||
Keyboard layout methods affects translation of letter to key code. They affect `sendLetter`, `sendText`, `batchTypeText` and `getKeyCode` methods. | ||
**cleanKeyboardLayout(): void** | ||
Resets the keyboard layout configuration. | ||
**setKeyboardLayout(newKeyMap: object): void** | ||
Sets a new keyboard layout. For example: | ||
var keyboardLayout = { | ||
'ç': '@192 c', | ||
'Ç': '@192 C' | ||
}; | ||
var ks = require('node-key-sender'); | ||
ks.aggregateKeyboardLayout(keyboardLayout); | ||
ks.sendText("Ç"); | ||
This keyboard layout converts letter 'Ç' to key codes '@192' and 'C'. | ||
**aggregateKeyboardLayout(objKeyMap: object): void** | ||
Appends the new mapping to the current mapping. | ||
**getKeyboardLayout(): object** | ||
Returns the current keyboard layout. | ||
## Other methods | ||
**getKeyCode(letter: string)** | ||
Gets the key code of a letter. A letter will be automatically converted to key code according to the keyboard layout configuration. You may set this configuration with `cleanKeyboardLayout`, `setKeyboardLayout` or `aggregateKeyboardLayout`. | ||
**setOption(optionName: string, optionValue: string)** | ||
Options that are passed as arguments to the jar. This is the list of valid options names: | ||
- startDelayMillisec (int): Delay in milliseconds it will wait to press the first key. | ||
- globalDelayPressMillisec (int): Global delay in milliseconds it will keep the key pressed. | ||
- globalDelayBetweenMillisec (int): Global delay in milliseconds it will wait until it presses the next key. | ||
- caseCorrection (boolean): When it is on, if you send key 'A' (in upper case), the jar will automatically hold Shift, so resulting key is 'A'. | ||
- extra (string): Use may use it to send raw arguments to the jar file. Example: ' -c 1 -d 1000'. | ||
**execute(arrParams: array): Promise** | ||
Use this method if you want to directly call the jar file. | ||
## Promises | ||
Some methods of this lib returns a promise: | ||
ks.sendKey('a').then( | ||
function(stdout, stderr) { | ||
// For success | ||
}, | ||
function(error, stdout, stderr) { | ||
// For error | ||
} | ||
); | ||
The promise is resolved or rejected right after the jar finishes its execution. | ||
List of methods that returns this promise: `sendCombination`, `sendKey`, `sendKeys`, `sendLetter`, `sendText`, `execute`, `sendBatch`. | ||
# List of key codes | ||
@@ -126,0 +284,0 @@ |
84221
225
469