Raphael Castro
Raphael Castro

Reputation: 1148

How do I listen for the operating system click event in node JS?

I am building an electron app where you can highlight a phone number where ever: email, browser, word etc... then right click and click on a custom menu item, then node would add that phone number to a list. I'm having trouble finding the documentation for the "node" click event, everything I'm finding is the browser or window click event. However I need this to happen regardless of the application I'm in. What is this called? Is there a better way to do this?

Upvotes: 0

Views: 810

Answers (1)

Raphael Castro
Raphael Castro

Reputation: 1148

After a few hours of research I came up with an acceptable solution, ADyson put me on the right path. Here is the resulting code if anyone needs to do something similar:

const { app, BrowserWindow, globalShortcut, clipboard } = require('electron');
const path = require('path');
const robot = require('robotjs');
const Notification1 = require('node-mac-notifier');

//removed opening window because it is irrelevant.

app.whenReady().then(() => {
    globalShortcut.register('Cmd+Shift+S', () => {
        robot.keyTap('c', 'command');
        const number = clipboard.readText();
        new Notification1(`${number} added to DNC`, {});
        //Number logic here
    });
});

If you are using electron you have to rebuild both the robotjs package and the node-mac-notifier packages here is how you do that:

in the terminal:

npm install --save-dev electron-rebuild
npx electron-rebuild -f -m node_modules/node-mac-notifier
npx electron-rebuild -f -m node_modules/robotjs

You might get a "no gyp or clt detected" error when rebuilding the packages, here is how you fix that:

xcode-select --print-path
sudo rm -r -f <path>
xcode-select --install

Upvotes: 1

Related Questions