197 lines
5.9 KiB
JavaScript
197 lines
5.9 KiB
JavaScript
// vim: et:ts=4:sw=4:sts=4:ft=js
|
|
/***
|
|
Ox.Keyboard
|
|
***/
|
|
|
|
(function() {
|
|
|
|
var buffer = '',
|
|
bufferTime = 0,
|
|
bufferTimeout = 1000,
|
|
// wrapped in function so it can be collapsed in text editor
|
|
keyNames = (function() {
|
|
return {
|
|
0: 'section',
|
|
8: 'backspace',
|
|
9: 'tab',
|
|
12: 'clear',
|
|
13: 'enter',
|
|
16: 'shift',
|
|
17: 'control',
|
|
18: 'alt',
|
|
20: 'capslock',
|
|
27: 'escape',
|
|
32: 'space',
|
|
33: 'pageup',
|
|
34: 'pagedown',
|
|
35: 'end',
|
|
36: 'home',
|
|
37: 'left',
|
|
38: 'up',
|
|
39: 'right',
|
|
40: 'down',
|
|
45: 'insert',
|
|
46: 'delete',
|
|
47: 'help',
|
|
48: '0',
|
|
49: '1',
|
|
50: '2',
|
|
51: '3',
|
|
52: '4',
|
|
53: '5',
|
|
54: '6',
|
|
55: '7',
|
|
56: '8',
|
|
57: '9',
|
|
65: 'a',
|
|
66: 'b',
|
|
67: 'c',
|
|
68: 'd',
|
|
69: 'e',
|
|
70: 'f',
|
|
71: 'g',
|
|
72: 'h',
|
|
73: 'i',
|
|
74: 'j',
|
|
75: 'k',
|
|
76: 'l',
|
|
77: 'm',
|
|
78: 'n',
|
|
79: 'o',
|
|
80: 'p',
|
|
81: 'q',
|
|
82: 'r',
|
|
83: 's',
|
|
84: 't',
|
|
85: 'u',
|
|
86: 'v',
|
|
87: 'w',
|
|
88: 'x',
|
|
89: 'y',
|
|
90: 'z',
|
|
//91: 'meta.left',
|
|
//92: 'meta.right',
|
|
91: 'meta',
|
|
//92: 'meta',
|
|
93: 'meta',
|
|
96: '0.numpad',
|
|
97: '1.numpad',
|
|
98: '2.numpad',
|
|
99: '3.numpad',
|
|
100: '4.numpad',
|
|
101: '5.numpad',
|
|
102: '6.numpad',
|
|
103: '7.numpad',
|
|
104: '8.numpad',
|
|
105: '9.numpad',
|
|
106: 'asterisk.numpad',
|
|
107: 'plus.numpad',
|
|
109: 'minus.numpad',
|
|
108: 'enter.numpad',
|
|
110: 'dot.numpad',
|
|
111: 'slash.numpad',
|
|
112: 'f1',
|
|
113: 'f2',
|
|
114: 'f3',
|
|
115: 'f4',
|
|
116: 'f5',
|
|
117: 'f6',
|
|
118: 'f7',
|
|
119: 'f8',
|
|
120: 'f9',
|
|
121: 'f10',
|
|
122: 'f11',
|
|
123: 'f12',
|
|
124: 'f13',
|
|
125: 'f14',
|
|
126: 'f15',
|
|
127: 'f16',
|
|
144: 'numlock',
|
|
145: 'scrolllock',
|
|
186: 'semicolon',
|
|
187: 'equal',
|
|
188: 'comma',
|
|
189: 'minus',
|
|
190: 'dot',
|
|
191: 'slash',
|
|
192: 'backtick',
|
|
219: 'openbracket',
|
|
220: 'backslash',
|
|
221: 'closebracket',
|
|
222: 'quote'
|
|
// see dojo, for ex.
|
|
};
|
|
})(),
|
|
modifierNames = {
|
|
altKey: 'alt', // mac: option
|
|
ctrlKey: 'control',
|
|
// metaKey: 'meta', // mac: command
|
|
shiftKey: 'shift'
|
|
};
|
|
|
|
Ox.UI.ready(function() {
|
|
// fixme: how to do this better?
|
|
// in firefox on mac, keypress doesn't fire for up/down
|
|
// if the cursor is at the start/end of an input element
|
|
// on linux, it doesn't seem to fire if the input element has focus
|
|
if ($.browser.mozilla) {
|
|
Ox.UI.$document.keypress(keypress);
|
|
Ox.UI.$document.keydown(function(event) {
|
|
var $element = $('input:focus');
|
|
if ($element.length) {
|
|
if (
|
|
(
|
|
keyNames[event.keyCode] == 'up' &&
|
|
$element[0].selectionStart + $element[0].selectionEnd == 0
|
|
) || (
|
|
keyNames[event.keyCode] == 'down' &&
|
|
$element[0].selectionStart == $element.val().length &&
|
|
$element[0].selectionEnd == $element.val().length
|
|
)
|
|
) {
|
|
keypress(event);
|
|
}
|
|
}
|
|
});
|
|
} else {
|
|
Ox.UI.$document.keydown(keypress);
|
|
}
|
|
});
|
|
|
|
function keypress(event) {
|
|
var focused = Ox.Focus.focused(),
|
|
key,
|
|
keys = [],
|
|
//ret = true,
|
|
time;
|
|
Ox.forEach(modifierNames, function(v, k) {
|
|
event[k] && keys.push(v);
|
|
});
|
|
// avoid pushing modifier twice
|
|
if (keyNames[event.keyCode] && keys.indexOf(keyNames[event.keyCode]) == -1) {
|
|
keys.push(keyNames[event.keyCode]);
|
|
}
|
|
key = keys.join('_');
|
|
if (key.match(/^[\w\d\-]$|SPACE/)) {
|
|
time = Ox.getTime();
|
|
if (time - bufferTime > bufferTimeout) {
|
|
buffer = '';
|
|
}
|
|
buffer += key == 'SPACE' ? ' ' : key;
|
|
bufferTime = time;
|
|
}
|
|
focused !== null && Ox.UI.elements[focused].triggerEvent('key_' + key);
|
|
if (['down', 'space', 'up'].indexOf(key) > -1 && !Ox.UI.elements[focused].hasClass('OxInput')) {
|
|
// prevent chrome from scrolling
|
|
return false;
|
|
}
|
|
/*
|
|
stack.forEach(function(v) {
|
|
// fixme: we dont get the return value!
|
|
ret = Ox.event.trigger(keyboard + Ox.toCamelCase(key) + '.' + v);
|
|
return ret;
|
|
});
|
|
*/
|
|
}
|
|
|
|
})();
|