Using Media Keys without using a global shortcut?


#1

I want to use media keys in my electron app, but only when I have focus on my application. I’ve tried adding them to a menu but that doesn’t seem to work. Is there a way to do this?

var app = require('app');
var globalShortcut = require('global-shortcut');
var Menu = require('menu');

app.on('ready', function() {
    var menu = Menu.buildFromTemplate([{
        label: 'Controls',
        submenu: [
            {label: 'Play', accelerator: 'MediaPlayPause', click: function() { console.log('this doesn\'t work') } },
            {label: 'Next', accelerator: 'ctrl+t', click: function() { console.log('this works fine')}}
        ]
    }]);

    Menu.setApplicationMenu(menu);

    globalShortcut.register('MediaNextTrack', function() { console.log('this hijacks all the media keys'); });
});

Something similar to this for Mac would be ideal: https://github.com/nevyn/SPMediaKeyTap


#2

Yeah, I would like to know that too


#3

Looks like you could use some JS detection of window focus

ar window_focus;

$(window).focus(function() {
    window_focus = true;
}).blur(function() {
    window_focus = false;
});

$(document).one('click', function() {
    setInterval(function() {
        $('body').append('has focus? ' + window_focus + '<br>');
    }, 1000);
});​