Skip to content

Instantly share code, notes, and snippets.

@BLamy
Created December 8, 2015 04:39
Show Gist options
  • Save BLamy/2aeb47b4d8e810fa29dd to your computer and use it in GitHub Desktop.
Save BLamy/2aeb47b4d8e810fa29dd to your computer and use it in GitHub Desktop.
'use strict';
const electron = require('electron');
const app = electron.app;
const globalShortcut = electron.globalShortcut;
// report crashes to the Electron project
require('crash-reporter').start();
// adds debug features like hotkeys for triggering dev tools and reload
require('electron-debug')();
// prevent window being garbage collected
let mainWindow;
function onClosed() {
// dereference the window
// for multiple windows store them in an array
mainWindow = null;
}
function createMainWindow() {
const win = new electron.BrowserWindow({
width: 600,
height: 400
});
win.loadURL(`file://${__dirname}/index.html`);
win.on('closed', onClosed);
return win;
}
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate-with-no-open-windows', () => {
if (!mainWindow) {
mainWindow = createMainWindow();
}
});
app.on('ready', () => {
// Register a 'ctrl+x' shortcut listener.
var ret = globalShortcut.register('ctrl+x', () => {
console.log('ctrl+x is pressed');
});
if (!ret) {
console.log('registration failed');
}
// Check whether a shortcut is registered.
console.log(globalShortcut.isRegistered('ctrl+x'));
});
app.on('will-quit', () => {
// Unregister a shortcut.
globalShortcut.unregister('ctrl+x');
// Unregister all shortcuts.
globalShortcut.unregisterAll();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment