Skip to content

Commit

Permalink
support global media keys for prev, next, play, pause and stop
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias Kahl committed Dec 1, 2015
1 parent 539f992 commit b182b71
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
26 changes: 26 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,32 @@
}
}, 500)

const ipcRenderer = require('electron').ipcRenderer;
ipcRenderer.on('playback-control', function(event, arg) {
var doc = document.querySelector("iframe").contentDocument

switch (arg) {
case "MediaPlayPause":
var playBtn = doc.querySelector("button.js-play")
var pauseBtn = doc.querySelector("button.js-pause")
if (pauseBtn.style.display == "none") playBtn.click()
else pauseBtn.click()
break;

case "MediaNextTrack":
doc.querySelector("button.js-next").click();
break;

case "MediaPreviousTrack":
doc.querySelector("button.js-previous").click();
break;

case "MediaStop":
doc.querySelector("button.js-pause").click();
break;
}
});

</script>
</body>
</html>
12 changes: 11 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,17 @@ app.on('ready', function() {
mainWindow.loadUrl('file://' + __dirname + '/index.html');

// Emitted when the window is closed.
mainWindow.on('closed', function() {
mainWindow.on('closed', function() {
mainWindow = null;
});

console.log("APP READY")

// handle media keys
var routeShortcuts = ["MediaPreviousTrack", "MediaNextTrack", "MediaPlayPause", "MediaStop"]
routeShortcuts.forEach(shortcut => {
globalShortcut.register(shortcut, function() {
mainWindow.webContents.send("playback-control", shortcut)
})
})
});

0 comments on commit b182b71

Please sign in to comment.