0

I have an exe file which can detect F6 key whenever i press in browser. But i want to do this task programatically in javascript. I have tried different methods from stackoverflow but none of them is detectable by exe file. But when i press F6 key manually then exe file detects.

These are the methods i have tried but none of them worked for me:

Javascript - trigger specific keyboard keys

Is it possible to simulate key press events programmatically?

etc.

After this automatic trigger, exe file return F7 key and i have to detect it in browser using javascript.

Afnan A.
  • 41
  • 2
  • 11
  • 1
    js is sandboxed to send the keypress to the javascript engine running in the browser, it won't send a keypress to the OS where it would/could be picked up by your external monitor. You need another exe that sends the keypress. – freedomn-m Jan 26 '22 at 14:37
  • @freedomn-m can i use nodejs for this purpose ? – Afnan A. Jan 26 '22 at 15:30
  • @freedomn-m But can i open browser help by triggering F1 key programatically in javascript ? its belongs to browser now not any external windows. I mean is there any method to run actual functionality of functions keys with javascript ? – Afnan A. Jan 26 '22 at 15:59

1 Answers1

0

This is done with nodejs by using https://www.npmjs.com/package/node-key-sender

node-key-sender is good to trigger keyboard keys automatically, see the code below

const http = require('http');
var ks = require('node-key-sender');
const requestListener = function (req, res) {
  res.writeHead(200);
  res.end('Server is running under 9999 port');

  ks.sendKey('F6');
}
const server = http.createServer(requestListener);
server.listen(9999);

In response, i can detect any key trigger by https://www.npmjs.com/package/iohook

Finally my code is:

const http = require('http');
var ks = require('node-key-sender');
const ioHook = require('iohook');

const requestListener = function (req, res) {
  res.writeHead(200);
  res.end('Server is running under 9999 port');

  ioHook.on("keydown", event => {
      console.log(event);
      // {keychar: 'f', keycode: 19, rawcode: 15, type: 'keypress'}
    });
    ioHook.start();

  ks.sendKey('F6');
}

const server = http.createServer(requestListener);
server.listen(9999);
Afnan A.
  • 41
  • 2
  • 11