forked from borismus/Chrome-Media-Keys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey.js
39 lines (35 loc) · 1.16 KB
/
key.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Gets injected into all pages for capturing keyboard events.
// TODO: find a good solution for when there's no page focus
// Get keyboard shortcuts for various events on startup.
var previous = {keyCode: 117};
var playpause = {keyCode: 118};
var next = {keyCode: 119};
chrome.extension.sendRequest({type: 'key'}, function(response) {
previous = response.previous;
playpause = response.playpause;
next = response.next;
});
window.onkeydown = function(event) {
var command = null;
if (eventKeyMatch(event, previous)) {
command = 'previous';
} else if (eventKeyMatch(event, playpause)) {
command = 'playpause';
} else if (eventKeyMatch(event, next)) {
command = 'next';
}
if (command !== null) {
var request = {type: 'command', command: command};
// Send message to music player
chrome.extension.sendRequest(request, function(response) {
// Do stuff on successful response
});
}
};
function eventKeyMatch(event, key) {
// Returns true iff the event matches the given key
return event.keyCode == key.keyCode &&
event.altKey == key.altKey &&
event.ctrlKey == key.ctrlKey &&
event.shiftKey == key.shiftKey;
}