forked from traprajith/open-school-CE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapslock.js
111 lines (89 loc) · 3.09 KB
/
capslock.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Capslock 0.4 - jQuery plugin to detect if a user's caps lock is on or not.
*
* Provides events, "caps_lock_on" and "caps_lock_off", that custom functions can be bound to.
* The capslock function can be called on a specific element, or a set of elements:
* $("#my_textarea").capslock(options); // One textarea
* $("textarea").capslock(options); // All textareas
*
* Copyright (c) 2009 Arthur McLean
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* Special thanks to driehle and Adrian D. Alvarez for sending in contributions that supurred
* further development.
*
*/
; // Just in case the previously included plug-in failed to close with a semi-colon.
(function($) {
$.fn.capslock = function(options) {
if (options) $.extend($.fn.capslock.defaults, options);
this.each(function() {
$(this).bind("caps_lock_on", $.fn.capslock.defaults.caps_lock_on);
$(this).bind("caps_lock_off", $.fn.capslock.defaults.caps_lock_off);
$(this).bind("caps_lock_undetermined", $.fn.capslock.defaults.caps_lock_undetermined);
$(this).keypress(function(e){
check_caps_lock(e);
});
});
return this;
};
// The actual check:
function check_caps_lock(e) {
var ascii_code = e.which;
var letter = String.fromCharCode(ascii_code);
var upper = letter.toUpperCase();
var lower = letter.toLowerCase();
var shift_key = e.shiftKey;
// If the upper and lower case characters are the same, then we have no way to know if capslock is on
if(upper !== lower) {
if( letter === upper && !shift_key ) {
$(e.target).trigger("caps_lock_on");
} else if( letter === lower && !shift_key ) {
$(e.target).trigger("caps_lock_off");
} else if( letter === lower && shift_key) {
// Must be a Windows user!
$(e.target).trigger("caps_lock_on");
} else if( letter === upper && shift_key ) {
// Either caps lock is off, or the caps lock is on and they are on a Mac
if( navigator.platform.toLowerCase().indexOf("win") !== -1 ) {
// You are on Windows, so we know caps lock is off
$(e.target).trigger("caps_lock_off");
} else {
// Ug, we don't know
if( navigator.platform.toLowerCase().indexOf("mac") !== -1
&&
$.fn.capslock.defaults.mac_shift_hack ) {
// Assue that the caps lock is off
$(e.target).trigger("caps_lock_off");
} else {
$(e.target).trigger("caps_lock_undetermined");
}
}
} else {
$(e.target).trigger("caps_lock_undetermined");
}
} else {
$(e.target).trigger("caps_lock_undetermined");
}
if($.fn.capslock.defaults.debug) {
if(console) {
console.log("Ascii code: " + ascii_code);
console.log("Letter: " + letter);
console.log("Upper Case: " + upper);
console.log("Shift key: " + shift_key);
}
}
}
// Public definition of defaults for easy overriding:
$.fn.capslock.defaults = {
caps_lock_on: function() {},
caps_lock_off: function() {},
caps_lock_undetermined: function() {},
mac_shift_hack: true,
debug: false
};
})(jQuery);
// JavaScript Document