Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardX366 committed Jun 12, 2021
1 parent 2ed93f7 commit cdb5469
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 52 deletions.
118 changes: 73 additions & 45 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ module.exports = async function(path, hotkeysList, options) {
const ahk = {
defaultColorVariation: 0,
hotkeysList: hotkeysList,
width: 1366,
height: 768,
current: null,
hotkeys: {},
hotkeysPending: [],
Expand All @@ -61,7 +63,7 @@ module.exports = async function(path, hotkeysList, options) {
* @param {string | object} key - The hotkey to bind
* @param {function} run - The function to run on bind
*/
setHotkey: function(key, run) {
setHotkey(key, run) {
var ahkKey;
if (typeof key === "string") ahkKey = key;
else {
Expand All @@ -76,7 +78,9 @@ module.exports = async function(path, hotkeysList, options) {
.replace("shift", "+")
.replace("any", "*")
}
ahkKey = mod + key.key;
ahkKey = mod + key.key
.replace(/\\{/g, "{{}")
.replace(/\\}/g, "{}}");
}
}
ahk.hotkeys[ahkKey] = run;
Expand All @@ -86,15 +90,15 @@ module.exports = async function(path, hotkeysList, options) {
* @param {number} x - The time in ms to sleep for
* @returns A promise that is fufilled once the time is up
*/
sleep: function(x) {
sleep(x) {
return new Promise(function(resolve) {
setTimeout(resolve, x);
});
},
/**
* Runs a hotkey if one is detected
*/
waitForInterrupt: async function() {
async waitForInterrupt() {
while (ahk.hotkeysPending[0]) {
await ahk.hotkeys[ahk.hotkeysPending[0]]();
ahk.hotkeysPending.shift();
Expand All @@ -109,8 +113,8 @@ module.exports = async function(path, hotkeysList, options) {
* positioning?: number
* }} x - The Parameters
*/
mouseMove: async function(x) {
if (!x.time) x.time = 2;
async mouseMove(x) {
if (!x.time) x.time = "";
if (x.positioning === "%") {
x.x = Math.floor(x.x / 100 * ahk.width);
x.y = Math.floor(x.y / 100 * ahk.height);
Expand All @@ -121,27 +125,43 @@ module.exports = async function(path, hotkeysList, options) {
/**
* Clicks the mouse. Look at the documentation for information on parameters.
* @param {{
* x?: number,
* y?: number,
* positioning?: string
* button?: string,
* state?: string
* }} x
* state?: string,
* count?: number
* }} x - The parameters
*/
click: async function(x) {
async click(x) {
if (!x) {
x = {};
}
if (!x.button) x.button = "left";
if (!x.x || !x.y) {
x.x = "";
x.y = "";
}
if (x.positioning === "%" && x.x) {
x.x = Math.floor(x.x / 100 * ahk.width);
x.y = Math.floor(x.y / 100 * ahk.height);
}
if (x.button === "left") x.button = "L";
else if (x.button === "middle") x.button = "M";
else if (x.button === "right") x.button = "R";
else x.button = "";
if (x.state === "down") x.state = "D";
else if (x.state === "up") x.state = "U";
else x.state = "";
runner.stdin.write(`click;${x.button};${x.state}\n`);
if (!x.count) x.count = "";
runner.stdin.write(`click;${x.x} ${x.y} ${x.button} ${x.state} ${x.count}\n`);
await wait();
},
/**
* Gets or sets the clipboard
* @param {string} [x] - If provided, the clipboard is set to the value
* @returns The clipboard if no parameters are passed in
*/
clipboard: async function(x) {
async clipboard(x) {
if (x) {
runner.stdin.write(`setClipboard;${x}\n`);
await wait();
Expand All @@ -150,24 +170,6 @@ module.exports = async function(path, hotkeysList, options) {
return await wait();
}
},
/**
* Types out a string. Look at documentation for extra information.
* @param {string} x - The string to send
*/
send: async function(x) {
var toSend = "";
if (x.blind) toSend += "{Blind}";
toSend += x.msg
.replace(/!/g, "{!}")
.replace(/#/g, "{#}")
.replace(/\+/g, "{+}")
.replace(/\^/g, "{^}")
.replace(/\\{/g, "{{}")
.replace(/\\}/g, "{}}")
.replace(/\n/g, "{Enter}");
runner.stdin.write(`send;${toSend}\n`);
await wait();
},
/**
* Searches for a pixel of set color
* @param {{
Expand All @@ -180,7 +182,7 @@ module.exports = async function(path, hotkeysList, options) {
* }} x - The parameters
* @returns If found, [x, y]. If % positioning is used, it returns them as screen percents.
*/
pixelSearch: async function(x) {
async pixelSearch(x) {
if (!x.variation) x.variation = ahk.defaultColorVariation;
if (x.positioning === "%") {
x.x1 = Math.floor(x.x1 / 100 * ahk.width);
Expand Down Expand Up @@ -209,7 +211,7 @@ module.exports = async function(path, hotkeysList, options) {
* }} x - The parameters
* @returns The pixel's color in hex RGB
*/
getPixelColor: async function(x) {
async getPixelColor(x) {
if (x.positioning === "%") {
x.x = Math.floor(x.x / 100 * ahk.width);
x.y = Math.floor(x.y / 100 * ahk.height);
Expand All @@ -229,7 +231,7 @@ module.exports = async function(path, hotkeysList, options) {
* @param {string} [x]
* @returns [x, y] If % positioning is used, they are returned as screen percents.
*/
getMousePos: async function(x) {
async getMousePos(x) {
runner.stdin.write(`getMousePos\n`);
var pos = (await wait()).split(" ");
if (x === "%") {
Expand All @@ -251,7 +253,7 @@ module.exports = async function(path, hotkeysList, options) {
* }} x - The parameters
* @returns If found, [x, y]. If % positioning is used, it returns them as screen percents.
*/
imageSearch: async function(x) {
async imageSearch(x) {
if (!x.variation) x.variation = ahk.defaultColorVariation;
else x.variation = `*${x.variation} `;
if (!x.trans) x.trans = "";
Expand Down Expand Up @@ -281,49 +283,72 @@ module.exports = async function(path, hotkeysList, options) {
* play?: number
* }} x - The parameters
*/
setKeyDelay: async function(x) {
async setKeyDelay(x) {
if (!x.delay) x.delay = "";
if (!x.duration) x.duration = "";
if (x.play) x.play = "Play";
else x.play = "";
runner.stdin.write(`setKeyDelay;${x.delay};${x.duration};${x.play}\n`);
await wait();
},
/**
* Types out a string. Look at documentation for extra information.
* @param {string} x - The string to send
*/
async send(x) {
var toSend = "{Text}";
if (x.blind) toSend += "{Blind}";
toSend += x.msg
.replace(/!/g, "{!}")
.replace(/#/g, "{#}")
.replace(/\+/g, "{+}")
.replace(/\^/g, "{^}")
.replace(/\\{/g, "{{}")
.replace(/\\}/g, "{}}");
runner.stdin.write(`send;${toSend}\n`);
await wait();
},
/**
* Types out a string using SendInput. Look at documentation for extra information.
* @param {string} x - The string to send
*/
sendInput: async function(x) {
var toSend = "";
async sendInput(x) {
var toSend = "{Text}";
if (x.blind) toSend += "{Blind}";
toSend += x.msg
.replace(/!/g, "{!}")
.replace(/#/g, "{#}")
.replace(/\+/g, "{+}")
.replace(/\^/g, "{^}")
.replace(/\\{/g, "{{}")
.replace(/\\}/g, "{}}")
.replace(/\n/g, "{Enter}");
.replace(/\\}/g, "{}}");
runner.stdin.write(`sendInput;${toSend}\n`);
await wait();
},
/**
* Types out a string using SendPlay. Look at documentation for extra information.
* @param {string} x - The string to send
*/
sendPlay: async function(x) {
var toSend = "";
async sendPlay(x) {
var toSend = "{Text}";
if (x.blind) toSend += "{Blind}";
toSend += x.msg
.replace(/!/g, "{!}")
.replace(/#/g, "{#}")
.replace(/\+/g, "{+}")
.replace(/\^/g, "{^}")
.replace(/\\{/g, "{{}")
.replace(/\\}/g, "{}}")
.replace(/\n/g, "{Enter}");
.replace(/\\}/g, "{}}");
runner.stdin.write(`sendPlay;${toSend}\n`);
await wait();
},
/**
* Sets the default mouse speed for clicks and mouseMove
* @param {number} x - The mouse speed from 0 - 100
*/
async setMouseSpeed(x) {
runner.stdin.write(`setMouseSpeed;${x}\n`);
await wait();
}
};
if (options.defaultColorVariation) {
Expand Down Expand Up @@ -362,8 +387,11 @@ write(x) {
.replace("shift", "+")
.replace("any", "*")
}
ahk.hotkeys[mod + x.key] = function() {};
hotkeysString += `${mod + x.key}::write("${mod + x.key}")
var key = x.key
.replace(/\\{/g, "{{}")
.replace(/\\}/g, "{}}");
ahk.hotkeys[mod + key] = function() {};
hotkeysString += `${mod + key}::write("${mod + key}")
`;
}
}
Expand Down
54 changes: 51 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,69 @@

# AHK NodeJS

AHK NodeJS allows users to communicate with AutoHotKey using NodeJS.

## Contents

* [Installation](#Installation)
* [Initiation](#Initiation)

## Installation

Use [npm](https://www.npmjs.com) to install AHK NodeJS.
If AutoHotKey.exe is not installed on your computer, please install it or refer to another npm module, ahk.exe.
If AutoHotKey.exe is not installed on your computer, please install it or refer to another npm module, [ahk.exe](https://www.npmjs.com/package/ahk.exe).

```bash
npm i ahknodejs
```

## Usage

### Important Notes

- When a parameter is followed by a question mark, it means it is optional
- If curly brackets are to be used in strings, the must be escaped as such: "\\\\{".
- Special characters in strings can be typed the same way as in AutoHotKey, as shown [here](https://www.autohotkey.com/docs/commands/Send.htm#keynames).

### Initiation

```js
require("ahknodejs");
require("ahknodejs")(Path, Hotkeys?, Options?);
```
**Path** - A string representing the location of AutoHotKey.exe
**Hotkeys** - A list of either strings and/or objects representing the hotkeys that will be used. The hotkeys will need to be [set](#setHotkey) in order to be used.
- If a string is used, it will be formatted the same way AutoHotKey hotkeys are formatted. This is further explained [here](https://www.autohotkey.com/docs/Hotkeys.htm).
- If an object is used, it be one of two forms depending on if multiple keys are used in the hotkey.
Note: if noInterrupt is set to true, the keys will still be sent when the hotkey is triggered. This option defaults to false.
One Key:
```js
{
"key": "KEY TO LISTEN",
"modifiers": [
"list of modifiers"
],
"noInterrupt": true or false
}
```
Modifiers that can be used are win, alt, control, shift, and any (any modifier will trigger the hotkey). This defaults to an empty array if not provided.
Multiple Keys:
```js
{
"keys": [
"KEY TO LISTEN",
],
"noInterrupt": true or false
}
```
Note: If a multi-key hotkey is used, it will trigger no matter what modifers are present in accordance with [AutoHotKey](https://www.autohotkey.com/docs/Hotkeys.htm#combo).
**Options** - An object with the options to set for AHKNodeJS. The properties are listed below:
- defaultColorVariation: If the color variation is not set for [imageSearch](#imageSearch) and [pixelSearch](#pixelSearch), this value is automatically used. (Defaults to 0)
## License
[MIT](https://github.com/Richard-X-366/AHKNodeJS/blob/master/LICENSE/)
[MIT](https://github.com/Richard-X-366/AHKNodeJS/blob/master/LICENSE/)
14 changes: 10 additions & 4 deletions runner.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ Loop {
MouseMove, data[2], data[3], data[4]
write("done")
} else if (data[1] = "click") {
MouseClick, % data[2],,,,% data[3]
Click % data[2]
write("done")
} else if (data[1] = "clickPlay") {
SendPlay % "{Click" data[2] "}"
write("done")
} else if (data[1] = "getClipboard") {
write(clipboard)
} else if (data[1] = "setClipboard") {
clipboard = % data[2]
write("done")
} else if (data[1] = "send") {
Send % data[2]
write("done")
} else if (data[1] = "pixelSearch") {
PixelSearch, x, y, data[2], data[3], data[4], data[5], data[6], data[7], Fast RGB
write(x " " y)
Expand All @@ -57,11 +57,17 @@ Loop {
} else if (data[1] = "setKeyDelay") {
setKeyDelay, data[2], data[3], % data[4]
write("done")
} else if (data[1] = "send") {
Send % data[2]
write("done")
} else if (data[1] = "sendInput") {
SendInput % data[2]
write("done")
} else if (data[1] = "sendPlay") {
SendPlay % data[2]
write("done")
} else if (data[1] = "setMouseSpeed") {
SetDefaultMouseSpeed % data[2]
write("done")
}
}

0 comments on commit cdb5469

Please sign in to comment.