2

I would like to know how to get a certain key on the keyboard to be pressed via code on nodejs.

For instance, I want the f3 button to pressed once the following page is rendered:

var express = require('express');
var router = express.Router();


/* GET home page. */
router.get('/', function(req, res, next) {
   // press key
});

module.exports = router;
code_legend
  • 3,547
  • 15
  • 51
  • 95
  • Possible duplicate of [nodejs how to read keystrokes from stdin](http://stackoverflow.com/questions/5006821/nodejs-how-to-read-keystrokes-from-stdin) – node_modules Apr 10 '17 at 12:20
  • Take a look at http://stackoverflow.com/a/17473563/7707749, it might be what you search for. – King Reload Apr 10 '17 at 12:21
  • I don't think this is possible. Certainly not with the server-side code that you're demonstrating. And while I've not done the research, I can't imagine that browser JS's sandbox would allow direct hardware simulation like that either. Why do you need F3 hit? What do you expect to happen? Are you assuming all computer F3 buttons do the same thing? – Paul Apr 10 '17 at 12:23
  • 2
    @codekid and King Reload> read the question. He is not trying to read keyboard input, he's trying to *cause* the F3 to be hit. – Paul Apr 10 '17 at 12:24

3 Answers3

3

Take a look at robotjs, which can be used to generate keyboard events.

For instance, to "send" an F3 key press:

const robot = require('robotjs');
...
router.get('/', function(req, res, next) {
  robot.keyTap('f3');
  res.end();
});

Although it depends on which OS you're using if this is going to work as expected.

robertklep
  • 198,204
  • 35
  • 394
  • 381
0

Not possible from server side. You can include a javascript script in your page which can trigger the event. If you are using jQuery.

var ev = jQuery.Event("keypress");
ev.ctrlKey = false;
ev.which = 37;
$("container").trigger(ev);

Is it possible to trigger a keyboard button with JavaScript?

Community
  • 1
  • 1
Sanchay Kumar
  • 566
  • 5
  • 11
  • Thank you I have added $('#close-btn').click(function(){ var ev = jQuery.Event("keypress"); ev.ctrlKey = false; ev.which = 37; $("container").trigger(ev); }); for the following button – code_legend Apr 10 '17 at 12:40
  • but it doesnt seem to work when button click (f3 close the application) – code_legend Apr 10 '17 at 12:41
  • If your intention is for any hardware simulation, it is not possible. This will only trigger any event handler – Sanchay Kumar Apr 10 '17 at 12:43
0

You can simply use applescript in nodejs, and node-key-sender on other platform.

const os = require('os')
const childProcess = require('child_process')
const { promisify } = require('util')
const ks = require('node-key-sender')  

function hitHotkey (key, modifier) {
    if (os.type() === 'Darwin') {
      if (modifier) {
        return exec(`Script="tell app \\"System Events\\" to keystroke ${key} using ${modifier} down"
        osascript -e "$Script"`)
      } else {
        return exec(`Script="tell app \\"System Events\\" to keystroke ${key}"
        osascript -e "$Script"`)
      }
    } else {
      if (modifier) {
        return ks.sendCombination([modifier, key])
      } else {
        return ks.sendKey(key)
      }
    }
  }
林东吴
  • 171
  • 1
  • 8