1 var $j = jQuery.noConflict();
4 buttonClass: "button", // default button class
5 onclick: "jsKeyboard.write();", // default onclick event for button
6 keyClass: "key", // default key class used to define style of text of the button
11 "keyboard": [], // different keyboards can be set to this variable in order to switch between keyboards easily.
12 init: function(elem, keyboard) {
13 jsKeyboard.keyboard["default"] = jsKeyboard.defaultKeyboard;
14 jsKeyboard.keyboardLayout = elem;
16 if (keyboard != null && keyboard != undefined)
17 jsKeyboard.generateKeyboard(keyboard);
19 jsKeyboard.generateKeyboard("default");
21 jsKeyboard.addKeyDownEvent();
24 $j(':input').not('[type="reset"]').not('[type="submit"]').on('focus, click', function(e)
26 jsKeyboard.currentElement = $j(this);
27 jsKeyboard.currentElementCursorPosition = $j(this).getCursorPosition();
28 console.log('keyboard is now focused on '+jsKeyboard.currentElement.attr('name')+' at pos('+jsKeyboard.currentElementCursorPosition+')');
32 jsKeyboard.currentElement = $j(t);
35 keyboardLayout: "", // it shows the html element where keyboard is generated
36 currentKeyboard: "default", // it shows the which keyboard is used. If it's not set default keyboard is used.
38 generateKeyboard: function(keyboard) {
45 s += "<div id=\"keyboard\">";
46 s += "<div id=\"keyboardHeader\">";
47 // s += "<div onclick=\"jsKeyboard.hide();\"><span>" + jsKeyboard.settings.text.close + "</span><span class=\"closex\"> X</span></div>"
51 s += "<div id=\"keyboardSmallLetter\">";
52 $j.each(jsKeyboard.keyboard[keyboard].smallLetter, function(i, key) {
58 s += "<div id=\"keyboardCapitalLetter\">";
59 $j.each(jsKeyboard.keyboard[keyboard].capitalLetter, function(i, key) {
65 s += "<div id=\"keyboardNumber\">";
66 $j.each(jsKeyboard.keyboard[keyboard].number, function(i, key) {
72 s += "<div id=\"keyboardSymbols\">";
73 $j.each(jsKeyboard.keyboard[keyboard].symbols, function(i, key) {
78 function generate(key) {
79 bClass = key.buttonClass == undefined ? jsKeyboard.settings.buttonClass : key.buttonClass;
80 kClass = key.keyClass == undefined ? jsKeyboard.settings.keyClass : key.keyClass;
81 onclick = key.onclick == undefined ? jsKeyboard.settings.onclick.replace("()", "(" + key.value + ")") : key.onclick;
83 text = (key.isChar != undefined || key.isChar == false) ? key.value : String.fromCharCode(key.value);
85 s += "<div class=\"" + bClass + "\" onclick=\"" + onclick + "\"><div class=\"" + kClass + "\">" + text + "</div></div>";
87 bClass = ""; kClass = ""; onclick = ""; text = "";
90 $j("#" + jsKeyboard.keyboardLayout).html(s);
92 addKeyDownEvent: function() {
93 $j("#keyboardCapitalLetter > div.button, #keyboardSmallLetter > div.button, #keyboardNumber > div.button, #keyboardSymbols > div.button").
94 bind('mousedown', (function() { $j(this).addClass("buttonDown"); })).
95 bind('mouseup', (function() { $j(this).removeClass("buttonDown"); })).
96 bind('mouseout', (function() { $j(this).removeClass("buttonDown"); }));
98 //key focus down on actual keyboard key presses
102 changeToSmallLetter: function() {
103 $j("#keyboardCapitalLetter,#keyboardNumber,#keyboardSymbols").css("display", "none");
104 $j("#keyboardSmallLetter").css("display", "block");
106 changeToCapitalLetter: function() {
107 $j("#keyboardCapitalLetter").css("display", "block");
108 $j("#keyboardSmallLetter,#keyboardNumber,#keyboardSymbols").css("display", "none");
110 changeToNumber: function() {
111 $j("#keyboardNumber").css("display", "block");
112 $j("#keyboardSymbols,#keyboardCapitalLetter,#keyboardSmallLetter").css("display", "none");
114 changeToSymbols: function() {
115 $j("#keyboardCapitalLetter,#keyboardNumber,#keyboardSmallLetter").css("display", "none");
116 $j("#keyboardSymbols").css("display", "block");
118 updateCursor: function()
120 //input cursor focus and position during typing
121 jsKeyboard.currentElement.setCursorPosition(jsKeyboard.currentElementCursorPosition);
124 var a = jsKeyboard.currentElement.val(),
125 b = String.fromCharCode(m),
126 pos = jsKeyboard.currentElementCursorPosition,
127 output = [a.slice(0, pos), b, a.slice(pos)].join('');
128 jsKeyboard.currentElement.val(output);
129 jsKeyboard.currentElementCursorPosition++; //+1 cursor
130 jsKeyboard.updateCursor();
133 var a = jsKeyboard.currentElement.val(),
134 pos = jsKeyboard.currentElementCursorPosition,
135 output = [a.slice(0, pos-1), a.slice(pos)].join('');
136 jsKeyboard.currentElement.val(output);
137 jsKeyboard.currentElementCursorPosition--; //-1 cursor
138 jsKeyboard.updateCursor();
141 var t = jsKeyboard.currentElement.val();
142 jsKeyboard.currentElement.val(t + "\n");
145 var a = jsKeyboard.currentElement.val(),
147 pos = jsKeyboard.currentElementCursorPosition,
148 output = [a.slice(0, pos), b, a.slice(pos)].join('');
149 jsKeyboard.currentElement.val(output);
150 jsKeyboard.currentElementCursorPosition++; //+1 cursor
151 jsKeyboard.updateCursor();
153 writeSpecial: function(m) {
154 var a = jsKeyboard.currentElement.val(),
155 b = String.fromCharCode(m),
156 pos = jsKeyboard.currentElementCursorPosition,
157 output = [a.slice(0, pos), b, a.slice(pos)].join('');
158 jsKeyboard.currentElement.val(output);
159 jsKeyboard.currentElementCursorPosition++; //+1 cursor
160 jsKeyboard.updateCursor();
163 $j("#keyboard").animate({ "bottom": "0" }, "slow", function() { });
166 $j("#keyboard").animate({ "bottom": "-350px" }, "slow", function() { });
172 { value: 81 },{ value: 87 },{ value: 69 },{ value: 82 },{ value: 84 },{ value: 89 },
173 { value: 85 },{ value: 73 },{ value: 79 },{ value: 80 },
174 { value: "Delete", isChar: "false", onclick: "jsKeyboard.del()", buttonClass: "button button_del", keyClass: "key key_del" },
176 { value: 65, buttonClass: "button button_a" },{ value: 83 },{ value: 68 },{ value: 70 },
177 { value: 71 },{ value: 72 },{ value: 74 },{ value: 75 },{ value: 76 },
178 { value: "Enter", isChar: "false", buttonClass: "button button_enter", onclick: "jsKeyboard.enter();", keyClass: "key key_enter" },
180 { value: "abc", isChar: "false", buttonClass: "button button_smallletter", onclick: "jsKeyboard.changeToSmallLetter();", keyClass: "key key_smallletter" },
181 { value: 90 },{ value: 88 },{ value: 67 },{ value: 86 },{ value: 66 },{ value: 78 },
182 { value: 77 },{ value: 44 },{ value: 46 },{ value: 64 },
184 { value: "123", isChar: "false", buttonClass: "button button_numberleft", onclick: "jsKeyboard.changeToNumber();", keyClass: "key key_number" },
185 { value: "Space", isChar: "false", buttonClass: "button button_space", onclick: "jsKeyboard.space();", keyClass: "key key_space" },
186 { value: "#%+", isChar: "false", buttonClass: "button button_symbolsright", onclick: "jsKeyboard.changeToSymbols();", keyClass: "key key_symbols" }
190 { value: 113 },{ value: 119 },{ value: 101 },{ value: 114 },{ value: 116 },
191 { value: 121 },{ value: 117 },{ value: 105 },{ value: 111 },{ value: 112 },
192 { value: "Delete", isChar: "false", onclick: "jsKeyboard.del()", buttonClass: "button button_del", keyClass: "key key_del" },
194 { value: 97, buttonClass: "button button_a" },{ value: 115 },{ value: 100 },{ value: 102 },
195 { value: 103 },{ value: 104 },{ value: 106 },{ value: 107 },{ value: 108 },
196 { value: "Enter", isChar: "false", buttonClass: "button button_enter", onclick: "jsKeyboard.enter();", keyClass: "key key_enter" },
198 { value: "ABC", isChar: "false", buttonClass: "button button_capitalletterleft", onclick: "jsKeyboard.changeToCapitalLetter();", keyClass: "key key_capitalletterleft" },
199 { value: 122 },{ value: 120 },{ value: 99 },{ value: 118 },{ value: 98 },
200 { value: 110 },{ value: 109 },{ value: 44 },{ value: 46 },{ value: 64 },
202 { value: "123", isChar: "false", buttonClass: "button button_numberleft", onclick: "jsKeyboard.changeToNumber();", keyClass: "key key_number" },
203 // { value: 32, buttonClass: "button button_space" },
204 { value: "Space", isChar: "false", buttonClass: "button button_space", onclick: "jsKeyboard.space();", keyClass: "key key_space" },
205 { value: "#%+", isChar: "false", buttonClass: "button button_symbolsright", onclick: "jsKeyboard.changeToSymbols();", keyClass: "key key_symbols" }
209 { value: 49 },{ value: 50 },{ value: 51 },{ value: 52 },{ value: 53 },{ value: 54 },
210 { value: 55 },{ value: 56 },{ value: 57 },{ value: 48 },
211 { value: "Delete", isChar: "false", onclick: "jsKeyboard.del()", buttonClass: "button button_del", keyClass: "key key_del" },
213 { value: 45, buttonClass: "button button_dash" },{ value: 47 },{ value: 58 },{ value: 59 },
214 { value: 40 },{ value: 41 },{ value: 36 },{ value: 38 },{ value: 64 },
215 { value: "Enter", isChar: "false", buttonClass: "button button_enter", onclick: "jsKeyboard.enter();", keyClass: "key key_enter" },
217 // { value: "ABC", isChar: "false", buttonClass: "button button_capitalletterleft", onclick: "jsKeyboard.changeToCapitalLetter()", keyClass: "key key_capitalletterleft" },
218 { value: "", isChar: "false", buttonClass: "button button_capitalletterleft", onclick: "", keyClass: "key" },
219 { value: 63 },{ value: 33 },{ value: 34 },{ value: 124 },{ value: 92 },{ value: 42 },{ value: 61 },{ value: 43 },
220 // { value: "ABC", isChar: "false", buttonClass: "button button_capitalletterright", onclick: "jsKeyboard.changeToCapitalLetter();", keyClass: "key key_capitalletterright" },
221 { value: "", isChar: "false", buttonClass: "button", onclick: "", keyClass: "key" },
222 { value: "", isChar: "false", buttonClass: "button", onclick: "", keyClass: "key" },
225 { value: "ABC", isChar: "false", buttonClass: "button button_numberleft", onclick: "jsKeyboard.changeToCapitalLetter();", keyClass: "key key_capitalletterleft" },
226 { value: "Space", isChar: "false", buttonClass: "button button_space", onclick: "jsKeyboard.space();", keyClass: "key key_space" },
227 { value: "#%+", isChar: "false", buttonClass: "button button_symbolsright", onclick: "jsKeyboard.changeToSymbols();", keyClass: "key key_symbols" }
231 { value: 91 },{ value: 93 },{ value: 123 },{ value: 125 },{ value: 35 },{ value: 37 },
232 { value: 94 },{ value: 42 },{ value: 43 },{ value: 61 },
233 { value: "Delete", isChar: "false", onclick: "jsKeyboard.del()", buttonClass: "button button_del", keyClass: "key key_del" },
235 { value: 95, buttonClass: "button button_underscore" },{ value: 92 },{ value: 124 },{ value: 126 },
236 { value: 60 },{ value: 62 },
237 { value: "€", isChar: "false", onclick: "jsKeyboard.writeSpecial('€');" },
238 { value: 163 },{ value: 165 },
239 { value: "Enter", isChar: "false", buttonClass: "button button_enter", onclick: "jsKeyboard.enter();", keyClass: "key key_enter" },
241 // { value: "ABC", isChar: "false", buttonClass: "button button_capitalletterleft", onclick: "jsKeyboard.changeToCapitalLetter();", keyClass: "key key_capitalletterleft" },
242 { value: "", isChar: "false", buttonClass: "button button_capitalletterleft", onclick: "", keyClass: "key" },
243 { value: 46 },{ value: 44 },{ value: 63 },{ value: 33 },{ value: 39 },{ value: 34 },{ value: 59 },{ value: 92 },
244 // { value: "ABC", isChar: "false", buttonClass: "button button_capitalletterright", onclick: "jsKeyboard.changeToCapitalLetter();", keyClass: "key key_capitalletterright" },
245 { value: "", isChar: "false", buttonClass: "button", onclick: "", keyClass: "key" },
246 { value: "", isChar: "false", buttonClass: "button", onclick: "", keyClass: "key" },
248 { value: "123", isChar: "false", buttonClass: "button button_numberleft", onclick: "jsKeyboard.changeToNumber();", keyClass: "key key_number" },
249 { value: "Space", isChar: "false", buttonClass: "button button_space", onclick: "jsKeyboard.space();", keyClass: "key key_space" },
250 { value: "ABC", isChar: "false", buttonClass: "button button_symbolsright", onclick: "jsKeyboard.changeToCapitalLetter();", keyClass: "key key_capitalletterleft" },
256 // GET CURSOR POSITION
257 jQuery.fn.getCursorPosition = function(){
258 if(this.length == 0) return -1;
259 return $j(this).getSelectionStart();
262 jQuery.fn.getSelectionStart = function(){
263 if(this.length == 0) return -1;
266 var pos = input.value.length;
268 if (input.createTextRange) {
269 var r = document.selection.createRange().duplicate();
270 r.moveEnd('character', input.value.length);
272 pos = input.value.length;
273 pos = input.value.lastIndexOf(r.text);
274 } //else if(typeof(input.selectionStart)!="undefined")
275 //pos = input.selectionStart;
280 //SET CURSOR POSITION
281 jQuery.fn.setCursorPosition = function(pos) {
282 this.each(function(index, elem) {
283 if (elem.setSelectionRange) {
284 elem.setSelectionRange(pos, pos);
285 } else if (elem.createTextRange) {
286 var range = elem.createTextRange();
287 range.collapse(true);
288 range.moveEnd('character', pos);
289 range.moveStart('character', pos);