-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.osk.js
190 lines (168 loc) · 4.7 KB
/
jquery.osk.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
(function($) {
//create a keyboard
//layoutURL: URL for the keyboard layout
//callback: each character generated by the keyboard will be passed to this function
$.fn.loadLayout = function(layoutURL, callback) {
var keyboard = new Keyboard($(this[0]));
callback = callback || false;
$.getJSON(layoutURL, function(layout) {
addKeys(layout, keyboard);
draw(keyboard);
});
if (callback)
keyboard.callback = callback;
return this;
};
//add keys to the keyboard
function addKeys(layout, keyboard) {
for (var i = 0; i < layout.length; i++) {
var row = [];
for (var j = 0; j < layout[i].length; j++) {
row[j] = new Key(layout[i][j]);
keyboard.keyIDs[row[j].id] = row[j];
}
keyboard.keys[i] = row;
}
}
//draw the keyboard
function draw(keyboard) {
keyboard.board.empty();
keyboard.board.removeClass("shift");
keyboard.board.removeClass("altGr");
keyboard.board.removeClass("capsLock");
for (var i = 0; i < keyboard.keys.length; i++) {
var row = $("<div></div>");
for (var j = 0; j < keyboard.keys[i].length; j++)
row.append(keyboard.keys[i][j].draw(keyboard));
keyboard.board.append(row);
}
}
//Keyboard object
function Keyboard(board) {
//DOM element which contains the keyboard
this.board = board;
//map ids to keys
this.keyIDs = {};
//callback for keypresses
this.callback = function(key) {alert(key)};
//state of the keyboard modifiers
this.modifiers = {
shift: false,
altGr: false,
capsLock: false
};
//jagged array of keys
this.keys = [];
}
//Key object
function Key(keyObj) {
//key data from the layout file
$.extend(this, keyObj);
//add defaults for missing values
this.sLabel = this.sLabel || this.label.toUpperCase();
this.altLabel = this.altLabel || this.label;
this.sAltLabel = this.sAltLabel || this.altLabel.toUpperCase();
this.func = this.func || "typelabel";
this.sFunc = this.sFunc || this.func;
this.isLetter = this.label.toUpperCase() != this.label.toLowerCase();
this.altIsLetter = this.altLabel.toUpperCase() != this.altLabel.toLowerCase();
}
//generate an HTML button for a key
Key.prototype.draw = function(keyboard) {
var button = this.button = $("<button id='" + this.id + "'>" + this.label + "</button>");
//wire click event to the keypress handler
this.button.click(function() {keyboard.keyIDs[this.id].activate(keyboard, button)});
return this.button;
};
//apply modifiers to the key's label
Key.prototype.modify = function(modifiers) {
var modsVal = (modifiers.shift?1:0) + (modifiers.altGr?2:0) + (modifiers.capsLock?4:0);
var text;
switch (modsVal) {
case 0:
text = this.label;
break;
case 1:
text = this.sLabel;
break;
case 2:
text = this.altLabel;
break;
case 3:
text = this.sAltLabel;
break;
//caps lock only effects letters
case 4:
text = this.isLetter ? this.sLabel : this.label;
break;
case 5:
text = this.isLetter ? this.label : this.sLabel;
break;
case 6:
text = this.isLetter ? this.sAltLabel : text = this.altLabel;
break;
case 7:
text = this.isLetter ? this.altLabel : text = this.sAltLabel;
break;
}
this.button.text(text);
};
//handle the keypress event
Key.prototype.activate = function(keyboard, button) {
if (keyboard.modifiers.shift)
handlers[this.sFunc](keyboard, button);
else
handlers[this.func](keyboard, button);
};
//handle keypresses
var handlers = {
space: function(keyboard) {
keyboard.callback(' ');
},
tab: function(keyboard) {
keyboard.callback('\t');
},
backspace: function(keyboard) {
keyboard.callback('\b');
},
left: function(keyboard) {
keyboard.callback('\3');
},
right: function(keyboard) {
keyboard.callback('\4');
},
enter: function(keyboard) {
keyboard.callback('\n');
},
shift: function(keyboard) {
keyboard.modifiers.shift = !keyboard.modifiers.shift;
keyboard.board.toggleClass("shift");
modify(keyboard);
return keyboard.modifiers.shift;
},
altgr: function(keyboard) {
keyboard.modifiers.altGr = !keyboard.modifiers.altGr;
keyboard.board.toggleClass("altGr");
modify(keyboard);
return keyboard.modifiers.altGr;
},
capslock: function(keyboard) {
keyboard.modifiers.capsLock = !keyboard.modifiers.capsLock;
keyboard.board.toggleClass("capsLock");
modify(keyboard);
return keyboard.modifiers.capsLock;
},
typelabel: function(keyboard, button) {
keyboard.callback(button.text());
if (keyboard.modifiers.shift)
this.shift(keyboard);
if (keyboard.modifiers.altGr)
this.altgr(keyboard);
}
}
//apply keyboard modifiers
function modify(keyboard) {
for (var id in keyboard.keyIDs)
keyboard.keyIDs[id].modify(keyboard.modifiers);
}
})(jQuery)