Skip to content

Instantly share code, notes, and snippets.

@twolfson
Last active March 27, 2020 20:29
Show Gist options
  • Save twolfson/0c6b8062c712ddabf882 to your computer and use it in GitHub Desktop.
Save twolfson/0c6b8062c712ddabf882 to your computer and use it in GitHub Desktop.
Proof of concept to explore media keys for https://github.com/twolfson/google-music-electron/issues/31
node_modules/

gist-electron-media-keys

Proof of concept to explore media keys for twolfson/google-music-electron#31

Getting Started

To use this gist, perform the following steps:

# Clone the repository
git clone https://gist.github.com/0c6b8062c712ddabf882.git gist-electron-media-keys
cd gist-electron-media-keys

# Install our dependencies
npm install

# Start our electron app
npm start
<!DOCTYPE html>
<html>
<head>
<title>gist-electron-media-keys</title>
</head>
<body>
<h1>gist-electron-media-keys</h1>
<p>
Welcome to <code>gist-electron-media-keys</code>. A proof of concept to explore media key bindings.
<br/>
If you look at your console, you should see messages like:
</p>
<pre><code>medianexttrack registration bound!
mediaplaypause registration failed
mediaprevioustrack registration failed
mediastop registration bound!</code></pre>
<p>
For successfully bound media keys, these will say <code>bound</code>. For unsuccessful bindings, it will say <code>failed</code>.
<br/>
For successfully bound ones, please try pressing the media keys and you will see messages like:
</p>
<pre><code>mediastop pressed</code></pre>
</body>
</html>
// Load in our dependencies
var app = require('app');
var BrowserWindow = require('browser-window');
var globalShortcut = require('global-shortcut');
// Report any crashes to Electron's servers
require('crash-reporter').start();
// When all Windows are closed
app.on('window-all-closed', function handleWindowsClosed () {
// If we are not on OS X, exit
// DEV: OSX requires users to quit via the menu/cmd+q
if (process.platform !== 'darwin') {
console.log('All windows closed. Exiting application');
app.quit();
} else {
console.log('All windows closed but not exiting because OSX');
}
});
// When Electron is done loading
app.on('ready', function handleReady () {
// Create our browser window for google.com
var windowOpts = {
height: 920,
width: 1024
};
var browserWindow = new BrowserWindow(windowOpts);
browserWindow.loadUrl('file://' + __dirname + '/index.html');
// Load our media keys
// Copied from https://gist.github.com/twolfson/0a03820e27583cc9ad6e
var registered = globalShortcut.register('medianexttrack', function () {
console.log('medianexttrack pressed');
});
if (!registered) {
console.log('medianexttrack registration failed');
} else {
console.log('medianexttrack registration bound!');
}
var registered = globalShortcut.register('mediaplaypause', function () {
console.log('mediaplaypause pressed');
});
if (!registered) {
console.log('mediaplaypause registration failed');
} else {
console.log('mediaplaypause registration bound!');
}
var registered = globalShortcut.register('mediaprevioustrack', function () {
console.log('mediaprevioustrack pressed');
});
if (!registered) {
console.log('mediaprevioustrack registration failed');
} else {
console.log('mediaprevioustrack registration bound!');
}
var registered = globalShortcut.register('mediastop', function () {
console.log('mediastop pressed');
});
if (!registered) {
console.log('mediastop registration failed');
} else {
console.log('mediastop registration bound!');
}
});
{
"name": "gist-electron-media-keys",
"version": "1.0.0",
"description": "Proof of concept to explore media keys for https://github.com/twolfson/google-music-electron/issues/31",
"main": "index.js",
"dependencies": {
"electron-prebuilt": "0.34.5"
},
"devDependencies": {},
"scripts": {
"start": "electron index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Todd Wolfson <[email protected]> (http://twolfson.com/)",
"license": "Unlicense"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment