Skip to content

Instantly share code, notes, and snippets.

@ef2k
Created May 28, 2018 02:23
Show Gist options
  • Save ef2k/f0fdeb2269efb53b642ebacd68d6ad36 to your computer and use it in GitHub Desktop.
Save ef2k/f0fdeb2269efb53b642ebacd68d6ad36 to your computer and use it in GitHub Desktop.
Electron: Sending channel message from Main to Renderer

In main/index.js,

import { globalShortcut } from 'electron'

// mainWindow is an instance of BrowserWindow
let mainWindow;

app.on('ready', () => {
  globalShortcut.on('CommandOrControl+,', () => {
    mainWindow.webContents.send('shortcut-settings')
  })
})

In renderer/main.js,

import { ipcRenderer } from 'electron'

ipcRenderer.on('shortcut-settings', () => {
  // do whatever...
  // maybe, you're using vue, if so tell the router to go to the settings page.
  router.replace('/settings')
})

https://github.com/electron/electron/blob/master/docs/api/web-contents.md#contentssendchannel-arg1-arg2-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment