I'm using node and running this code while I do some work. I'm trying to automate some clicks and snap my cursor back to its original spot, which is working well so far. I'm having issues assigning certain keys to execute the code, though. I want to use F5 to make this script execute instead of 's' or whatever, but when I log the output of F5, the value of the keychar is 0. All other F keys and a variety of other keys also result in this value, which makes extending the program difficult. What am I doing wrong?
'use strict';
var robot = require("robotjs");
var ioHook = require('iohook');
robot.setMouseDelay(20);
ioHook.on("keypress", event => {
if(event.keychar == '116') {
var currentPosition = robot.getMousePos();
console.log(currentPosition);
robot.moveMouse(1735, 1019);
robot.mouseClick("left");
robot.setMouseDelay(3);
robot.moveMouse(currentPosition.x, currentPosition.y);
}
console.log(event.keychar);
//{keychar: 'f', keycode: 19, rawcode: 15, type: 'keypress'}
});
ioHook.start();
I'm using robotjs and iohook.
https://robotjs.io/
https://github.com/WilixLead/iohook
Would love some help!
I think you should use event.rawcode == '65300'
if you are using F5
with Fn
key. But if you are using F5
without Fn
key the event.rawcode == '65474'
.
So your code will be like that:
var robot = require("robotjs");
var ioHook = require('iohook');
robot.setMouseDelay(50);
ioHook.on("keypress", event => {
if(event.rawcode == '65300'||event.rawcode == '65474') {
var currentPosition = robot.getMousePos();
console.log(currentPosition)
robot.moveMouse(1735, 1019);
robot.mouseClick("left");
robot.setMouseDelay(20);
robot.moveMouse(currentPosition.x, currentPosition.y);
}
});
ioHook.start();