forked from FabioMR/ng-caps-lock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathng-caps-lock.js
68 lines (55 loc) · 2.09 KB
/
ng-caps-lock.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
(function () {
'use strict';
angular.module('ngCapsLock', []).run(['$rootScope', '$document', '$window', '$timeout', function ($rootScope, $document, $window, $timeout) {
function setCapsLockOn (isOn) {
$timeout(function() {
$rootScope.isCapsLockOn = isOn;
});
}
function bindingForAppleDevice () {
$document.bind("keydown", function (event) {
if (event.keyCode === 20) { setCapsLockOn(true); }
});
$document.bind("keyup", function (event) {
if (event.keyCode === 20) { setCapsLockOn(false); }
});
$document.bind("keypress", function (event) {
var code = event.charCode || event.keyCode;
var shift = event.shiftKey;
if (code > 96 && code < 123) { setCapsLockOn(false); }
if (code > 64 && code < 91 && !shift) { setCapsLockOn(true); }
});
};
function bindingForOthersDevices () {
var isKeyPressed = true;
$document.bind("keydown", function (event) {
if (event.originalEvent && event.originalEvent.getModifierState) {
return setCapsLockOn(event.originalEvent.getModifierState('CapsLock'))
}
if (!isKeyPressed && event.keyCode === 20) {
isKeyPressed = true;
if ($rootScope.isCapsLockOn != null) { setCapsLockOn(!$rootScope.isCapsLockOn); }
}
});
$document.bind("keyup", function (event) {
if (event.keyCode === 20) { isKeyPressed = false; }
});
$document.bind("keypress", function (event) {
var code = event.charCode || event.keyCode;
var shift = event.shiftKey;
if (code > 96 && code < 123) { setCapsLockOn(shift); }
if (code > 64 && code < 91) { setCapsLockOn(!shift); }
});
};
// Once the window goes out of focus, we can't be sure of the caps lock state
// so we have to default to not showing.
$window.addEventListener('blur', function (event) {
setCapsLockOn(false);
});
if (/Mac|iPad|iPhone|iPod/.test(navigator.platform)) {
bindingForAppleDevice();
} else {
bindingForOthersDevices();
}
}]);
}());