I'm trying to use Typescript together with Electron and RobotJS. I'm a beginner with all of these technologies so I lack the deep understanding of what is happening behind the scenes so I cannot really connect the dots on this issue. Electron and my app are working fine, everything compiles, but RobotJS won't work. The error I get is the following:
ERROR in ./node_modules/robotjs/build/Release/robotjs.node 1:0
Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)
I tried rebuilding RobotJS for Electron but it still doesn't work. Is this a native node module? I'm not sure, gotta read up on it first to figure out what I have to do with it if it indeed is.
Is this even achievable? Thanks and have a lovely day!
LE: It would appear that I was trying to use robotjs that relies on node (main process) from angular (rendered process). When I moved what I was trying to do with robotjs from the angular component to main.ts that is ran by electron, it worked. I will try and find a way to use it from angular, I guess Inter-process Communication or something, since I couldn't find any other way for now. Still waiting for ideas since I'm kind of in the dark right now. Thanks!
Ok, figured this out.
Basically you cannot access Electron's Node.js API straight from Angular.
For that you need an awesome tool called ngx-electron
. Read about it here or just Google it.
After you install this tool you can just DI its service as most guides instruct you and then use Electron remote
to get access to robotjs
.
Basically this:
const robot = this._electronService.remote.require('robotjs');
// The example supplied by robotjs
robot.setMouseDelay(2);
const twoPI = Math.PI * 2.0;
const screenSize = robot.getScreenSize();
const height = (screenSize.height / 2) - 10;
const width = screenSize.width;
for (let x = 0; x < width; x++) {
const y = height * Math.sin((twoPI * x) / width) + height;
robot.moveMouse(x, y);
}
Might not be the best solution but with my current limited knowledge about these technologies this will have to do. I'm open to opinions tho.
Thanks for your time, have a nice one!