最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to press a key using nodejs? - Stack Overflow

programmeradmin0浏览0评论

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;

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;
Share Improve this question asked Apr 10, 2017 at 12:17 code_legendcode_legend 3,28517 gold badges55 silver badges97 bronze badges 4
  • Possible duplicate of nodejs how to read keystrokes from stdin – node_modules Commented Apr 10, 2017 at 12:20
  • Take a look at stackoverflow./a/17473563/7707749, it might be what you search for. – King Reload Commented Apr 10, 2017 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 puter F3 buttons do the same thing? – Paul Commented Apr 10, 2017 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 Commented Apr 10, 2017 at 12:24
Add a ment  | 

3 Answers 3

Reset to default 4

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.

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?

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)
      }
    }
  }
发布评论

评论列表(0)

  1. 暂无评论