oxjs/source/Ox.UI/js/Core/Ox.Keyboard.js

130 lines
5.4 KiB
JavaScript
Raw Normal View History

2011-04-23 16:45:50 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=js
2011-04-22 22:03:10 +00:00
/***
Ox.Keyboard
***/
(function() {
var buffer = '',
2011-05-01 12:32:07 +00:00
// the dot notation ('0.numpad') makes the keyboard event ('key_0.numpad')
// namespaced, so that binding to 'key_0' will catch 'key_0.numpad' too
keyNames = {
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',
// fixme: this is usually 91: window.left, 92: window.right, 93: select
91: 'meta.left', 92: 'meta.right', 93: 'meta.right',
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', 224: 'meta'
// see dojo, for ex.
},
// meta comes last so that we can differentiate between
// alt_control_shift_meta.left and alt_control_shift_meta.right
2011-04-22 22:03:10 +00:00
modifierNames = {
2011-05-01 12:32:07 +00:00
altKey: 'alt', // Mac: option
2011-04-22 22:03:10 +00:00
ctrlKey: 'control',
2011-05-01 12:32:07 +00:00
shiftKey: 'shift',
metaKey: 'meta', // Mac: command
},
resetTimeout, triggerTimeout;
2011-04-22 22:03:10 +00:00
2011-05-01 12:32:07 +00:00
/*
2011-04-22 22:03:10 +00:00
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);
}
});
2011-05-01 12:32:07 +00:00
*/
2011-04-22 22:03:10 +00:00
2011-05-01 12:32:07 +00:00
Ox.UI.ready(function() {
Ox.UI.$document.keydown(keydown)
});
function keydown(event) {
2011-04-22 22:03:10 +00:00
var focused = Ox.Focus.focused(),
key,
2011-05-01 12:32:07 +00:00
keyName = keyNames[event.keyCode] || '',
keyBasename = keyName.split('.')[0],
keys = keyName ? [keyName] : [];
2011-04-22 22:03:10 +00:00
Ox.forEach(modifierNames, function(v, k) {
2011-05-01 12:32:07 +00:00
// avoid pushing modifier twice
if (event[k] && keyBasename != v) {
keys.splice(-1, 0, v);
}
2011-04-22 22:03:10 +00:00
});
key = keys.join('_');
2011-05-01 12:32:07 +00:00
if (/^(shift_)?[a-z]$|^\d(\.numpad)?$|space/.test(key)) {
// don't register leading spaces or trailing double spaces
if (!(keyName == 'space' && (buffer == '' || / $/.test(buffer)))) {
buffer += keyName == 'space' ? ' ' : keyBasename;
Ox.print('buffer', buffer)
// clear the trigger timeout only if the key went into the buffer
clearTimeout(triggerTimeout);
triggerTimeout = setTimeout(function() {
Ox.print('buffer', buffer)
focused !== null && Ox.UI.elements[focused].triggerEvent('keys', {
keys: buffer
});
}, 250);
2011-04-22 22:03:10 +00:00
}
}
2011-05-01 12:32:07 +00:00
// clear the reset timeout even if the key didn't go into the buffer
clearTimeout(resetTimeout);
resetTimeout = setTimeout(function() {
buffer = '';
}, 1000);
2011-04-29 22:56:46 +00:00
if (focused !== null) {
Ox.UI.elements[focused].triggerEvent('key_' + key);
2011-05-01 12:32:07 +00:00
// prevent Chrome from going back in history, or scrolling
if (
2011-05-01 12:32:07 +00:00
['backspace', 'down', 'left', 'right', 'space', 'up'].indexOf(key) > -1 &&
!Ox.UI.elements[focused].hasClass('OxInput')
) {
2011-04-29 22:56:46 +00:00
return false;
}
2011-04-22 22:03:10 +00:00
}
}
})();