-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmacKeys.js
91 lines (90 loc) · 3.21 KB
/
macKeys.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* (c) Michael Zelensky 2015
*
* DESCRIPTION
*
* Ctrl, Shift, Alt, Cmd buttons detector for Mac
* to support corresponding event.ctrlKey, event.shiftKey, event.altKey which do not work on Mac
*
* LICENSE
*
* Distributed under MIT License
* You can use this code in your project without limitation, no matter if it is commercial or not,
* with all copyright marks intact
*
* USAGE
*
* Just include this code into your JS or HTML and then check if key is pressed in your code, e.g.:
*
* window.onclick = function (event) {
* if (event.ctrlKey || macKeys.ctrlKey) {
* //do something
* }
* }
*
*/
(function(){
var saywho, isMac, webkit, mozilla, opera, kC;
isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0;
window.macKeys = {
cmdKey : false,
ctrlKey : false,
shiftKey : false,
altKey : false,
reset : function(){
this.cmdKey = false;
this.ctrlKey = false;
this.shiftKey = false;
this.altKey = false;
}
};
if (isMac) {
//browser detection, originates from: http://stackoverflow.com/questions/2400935/browser-detection-in-javascript
saywho = (function(){
var ua = navigator.userAgent, tem,
M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
if (/trident/i.test(M[1])) {
tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
return { 'browser': 'IE', 'version': (tem[1] || '') };
}
if (M[1] === 'Chrome') {
tem = ua.match(/\b(OPR|Edge)\/(\d+)/);
//if(tem != null) return tem.slice(1).join(' ').replace('OPR', 'Opera');
if (tem != null) return {'browser':tem.slice(1)[0].replace('OPR', 'Opera'), 'version': tem.slice(1)[1]}
}
M = M[2] ? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
if ((tem = ua.match(/version\/(\d+)/i))!= null) M.splice(1, 1, tem[1]);
return { 'browser': M[0], 'version': M[1] };
})();
webkit = (saywho.browser === 'Chrome' || saywho.browser === 'Safari');
mozilla = saywho.browser === 'Firefox';
opera = saywho.browser === 'Opera';
window.onkeydown = function(e){
kC = e.keyCode;
if (((webkit || opera) && (kC === 91 || kC === 93)) || (mozilla && kC === 224)) {
macKeys.cmdKey = true;
} else if (kC === 16) {
macKeys.shiftKey = true;
} else if (kC === 17) {
macKeys.ctrlKey = true;
} else if (kC === 18) {
macKeys.altKey = true;
}
};
window.onkeyup = function(e){
kC = e.keyCode;
if (((webkit || opera) && (kC === 91 || kC === 93)) || (mozilla && kC === 224)) {
macKeys.cmdKey = false;
} else if (kC === 16) {
macKeys.shiftKey = false;
} else if (kC === 17) {
macKeys.ctrlKey = false;
} else if (kC === 18) {
macKeys.altKey = false;
}
};
window.onblur = function(){
macKeys.reset();
};
}
})();