How to set mouse click as global shortcut in electron app

Viewed 664

I want to trigger an action on double right-click of mouse when electron app is running in the background.
I read the documentation and seems like there are no globalshortcuts for mouse events.

Any other way to achieve this? perhaps some node module compatible with electron app?

2 Answers

Unfortunately, we can't achieve that yet.

As MarshallOfSound commented on this official issue

"globalShortcut intercepts the key combination globally and prevents any application from receiving those key events. If you blocked apps from receiving mouse button presses things would break everywhere very quickly "

https://github.com/electron/electron/issues/13964


For macOS, I'm currently using Keyboard Maestro App. I'm getting my mouse keys with this app and triggering a globalShortcut key combination register in my Electron App.

Maybe for Windows, AHK (auto hot keys)

I found this nice solution for HTML code

<script type = "text/javascript">
     const {remote} = require('electron')
     const {Menu, MenuItem} = remote

     const menu = new Menu()

     // Build menu one item at a time, unlike
     menu.append(new MenuItem ({
        label: 'MenuItem1',
        click() { 
           console.log('item 1 clicked')
        }
     }))

     menu.append(new MenuItem({type: 'separator'}))
     menu.append(new MenuItem({label: 'MenuItem2', type: 'checkbox', checked: true}))
     menu.append(new MenuItem ({
        label: 'MenuItem3',
        click() {
           console.log('item 3 clicked')
        }
     }))

     // Prevent default action of right click in chromium. Replace with our menu.
     window.addEventListener('contextmenu', (e) => {
        e.preventDefault()
        menu.popup(remote.getCurrentWindow())
     }, false)
  </script>

Put this as first item in your HTML Body and it should work. At least it worked on my project

EDIT, cause I forgot it: Credits to google for answer on 6th entry

Related