Using Node.js for stdout, how to listen stdin in AHKv2?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
rhasselle
Posts: 4
Joined: 21 Mar 2023, 20:34

Using Node.js for stdout, how to listen stdin in AHKv2?

Post by rhasselle » 21 Mar 2023, 20:54

Hi, I'm trying to send "Up", "Down", "Right", "Left", or "Stop" from node.js to AHKv2.
But not sure how for AHK to listen to those inputs.

From node side, I've gathered that I can do something like this:

Code: Select all

import { ChildProcessWithoutNullStreams, spawn } from "child_process";

const child = spawn("C:/Program Files/AutoHotkey/v2/AutoHotkey64.exe", ["C:/scripts/AutoHotkey/v2/myScript.ahk"]);

and then I think I can do something like this:
child.stdin.write("Up" + "\n");

  setTimeout(() => {
    child.stdin.write("Left" + "\n");
  }, 1000);

  setTimeout(() => {
    child.stdin.write("Stop" + "\n");
  }, 2000);

  setTimeout(() => {
    child.kill();
  }, 3000);
From AHK side, how would I listen for these things so that the script stays open and listens? I was also looking into IPC/pipe, but trying to just figure out the stdin/stdout way first. Any help or pointing to helpful documentation would be appreciated.

geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: Using Node.js for stdout, how to listen stdin in AHKv2?

Post by geek » 22 Mar 2023, 08:38

You have a lot of options available to you, but the right one is highly dependent on the greater context. I suspect you'd be best off to look into binding the AutoHotkey DLL from Node, but for now we can focus on the stdin thing.

It's easy, really:

Code: Select all

f := FileOpen("*", "r")

while line := f.ReadLine() {
    MsgBox line
}

User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Using Node.js for stdout, how to listen stdin in AHKv2?

Post by kczx3 » 22 Mar 2023, 10:51

I’m sure it’s just an oversight but the second arg to FileOpen should be a string

geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: Using Node.js for stdout, how to listen stdin in AHKv2?

Post by geek » 22 Mar 2023, 10:57

kczx3 wrote:
22 Mar 2023, 10:51
I’m sure it’s just an oversight but the second arg to FileOpen should be a string
My bad, was writing from my phone. Fixed now

User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Using Node.js for stdout, how to listen stdin in AHKv2?

Post by kczx3 » 22 Mar 2023, 11:33

geek wrote:
22 Mar 2023, 10:57
My bad, was writing from my phone. Fixed now
I know how that goes! Its tough!

rhasselle
Posts: 4
Joined: 21 Mar 2023, 20:34

Re: Using Node.js for stdout, how to listen stdin in AHKv2?

Post by rhasselle » 22 Mar 2023, 17:16

Thanks for the code snippet.
Hm, I can't reach the `MsgBox line` inside of the while loop though.
If I put a `MsgBox "test"` outside, it triggers.

Maybe node is not sending the stdin properly? I'll look more into it...

rhasselle
Posts: 4
Joined: 21 Mar 2023, 20:34

Re: Using Node.js for stdout, how to listen stdin in AHKv2?

Post by rhasselle » 22 Mar 2023, 18:05

Nevermind, it seems to be working! Unsure why it wasn't working, and then suddenly it now works :) Thanks!

rhasselle
Posts: 4
Joined: 21 Mar 2023, 20:34

Re: Using Node.js for stdout, how to listen stdin in AHKv2?

Post by rhasselle » 22 Mar 2023, 18:09

Thanks @geek . Got the stdin/stdout working with your code.

Btw, do you have more information on this part?
geek wrote:
22 Mar 2023, 08:38
...I suspect you'd be best off to look into binding the AutoHotkey DLL from Node, but for now we can focus on the stdin thing.
I'm just trying to make game commands that can live for the full duration I have the game open, and I can send direction keys in a mutually exclusive way, maybe triggering anywhere from every 1 second, to every 10 seconds. Do you think DLL approach is better than stdin/stdout? Thanks

Post Reply

Return to “Ask for Help (v2)”