Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.1K
Views
How to press a key using nodejs?

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;
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

about 4 years ago · Santiago Trujillo Report

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?

about 4 years ago · Santiago Trujillo Report

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)
      }
    }
  }
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!