Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: "write" keystroke event in process.stdin #43137

Closed
tony-go opened this issue May 18, 2022 · 6 comments
Closed

feat: "write" keystroke event in process.stdin #43137

tony-go opened this issue May 18, 2022 · 6 comments
Labels
feature request Issues that request new features to be added to Node.js. stale

Comments

@tony-go
Copy link
Member

tony-go commented May 18, 2022

What is the problem this feature will solve?

Hey Node.js family 💚

The purpose is to be able to simulate user keystrokes in process.stdin.

What is the feature you are proposing to solve the problem?

I observed that we could switch stdin to rawMode and listen for keypress events. Try to run this piece of code:

process.stdin.setRawMode(true);

process.stdin.on('keypress', function(str, key) {
  if (key.sequence === '\u0003') {
    process.exit();
  }

  console.log(str, key);
});

Now, press the right arrow of your keyboard and you should observe in the console:

undefined
{
  sequence: '\x1B[C',
  name: 'right',
  ctrl: false,
  meta: false,
  shift: false,
  code: '[C'
}

So, the idea is to be able to pass/write/append this keystroke event into process.stdin:

process.stdin.setRawMode(true);

// with `.write` API
process.stdin.write('\x1B[C', { keyevent: true }); // with sequence
process.stdin.write('[C', { keyevent: true }); // with code

// with a new `.emitSequence` API, only available in raw mode
process.stdin.emitSequence('\x1B[C');

// with a new `.emitCode` API, only available in raw mode
process.stdin.emitCode('[C');

// with a new `.emitEvent` API, only available in raw mode
process.stdin.emitEvent({
  sequence: '\x1B[C',
  name: 'right',
  ctrl: false,
  meta: false,
  shift: false,
  code: '[C'
});

(Maybe the .writeAPI should be reserved for String of Buffer, I probably make some confusion here, sorry for that)

What alternatives have you considered?

During my search, I found this tool: https://github.com/jordansissel/xdotool (and found a Node.js binding).

But I thought that it should more convenient to have it in Node.

@tony-go tony-go added the feature request Issues that request new features to be added to Node.js. label May 18, 2022
@bnoordhuis
Copy link
Member

Do I summarize correctly you want a way to inject key strokes?

I won't bore you with details but this is one of those seemingly simple things that gets horrendously complex in practice. I don't see Node.js adopting a general solution because you're usually better off with a narrower scoped custom solution like xdotool.

@tony-go
Copy link
Member Author

tony-go commented May 22, 2022

Hey @bnoordhuis 👋

Thanks for your answer 🔝

I have already tried to take a look at xdotool (I know that a binding already exists for node), but it was not obvious that I could solve my problem with it. I'll spend more time on it a give an update here.

I won't bore you with details

Please do ^^ Before sending this issue I was thinking that I mixed up apples and pears. Please let me know if it's the case :)

@bnoordhuis
Copy link
Member

So, one problem is that xdotool works on a completely different level (the X protocol) than the tty.

Key stroke injection for ttys is restricted to the byte-at-a-time TIOCSTI ioctl and that's sometimes disabled for security reasons (example), requires root privs when the tty is not your controlling tty, may not honor line discipline, etc.

And that's Unices. Windows is its own can of worms.

@tony-go
Copy link
Member Author

tony-go commented May 23, 2022

@bnoordhuis Thanks a lot for these insights ^^

@github-actions
Copy link
Contributor

There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

For more information on how the project manages feature requests, please consult the feature request management document.

@github-actions github-actions bot added the stale label Nov 20, 2022
@github-actions
Copy link
Contributor

There has been no activity on this feature request and it is being closed. If you feel closing this issue is not the right thing to do, please leave a comment.

For more information on how the project manages feature requests, please consult the feature request management document.

@targos targos moved this from Pending Triage to Stale in Node.js feature requests Apr 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. stale
Projects
None yet
Development

No branches or pull requests

2 participants