views:

21

answers:

1

Is there a way to access the "media keys" with Javascript from within a browser tab/window?

I am mainly interested in a google chrome solution.

Using the following code, there doesn't seem to be an event generated for the media keys:

<html>
<body onKeyDown="showKeyCode(event)">
    <script type="text/javascript">

        function showKeyCode(event) {
            alert(event.keyCode);
        }

    </script>
</body>
</html>

Am I missing something? Could I do better with a Google Chrome extension??

A: 

Here's a list of key codes from Microsoft; they include keys such as "VK_VOLUME_MUTE". The key code for VK_VOLUME_MUTE is listed as 0xAD. 0xAD is decimal is 173.

And sure enough, when I load the following and hit the mute button on my keyboard, the key code reported is 173. So they do work like any other key; it wouldn't surprise me, though, if the key codes are Windows-specific. It may take some experimenting.

<html>
<body>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
<script>
$(document).ready(function() {
    $(document).keydown(function(ev){
       alert(ev.keyCode);
    });
});
</script>

</body>
</html>
JacobM
I have no such luck on Chrome/Linux though...
jldupont
Ah; I tried it on Chrome/Windows. Yeah, like I said, it's probably OS-specific. Do you get no keydown event firing on Linux, or are you getting a different keycode?
JacobM
no event at all.
jldupont
Do the keys work otherwise -- I mean, do they actually change the volume, etc.? If so, you may be out of luck; if there's no event, there's no event. But if not, perhaps you're missing a driver?
JacobM