fix keyboard handler
This commit is contained in:
parent
853a402a7a
commit
ac50af0509
1 changed files with 44 additions and 32 deletions
|
@ -9,7 +9,7 @@
|
||||||
slash: '/',
|
slash: '/',
|
||||||
space: ' '
|
space: ' '
|
||||||
},
|
},
|
||||||
hasCallback = {},
|
keyboardCallbacks = {},
|
||||||
keyboardEventRegExp = /^key(\.[\w\d.]+)?$/,
|
keyboardEventRegExp = /^key(\.[\w\d.]+)?$/,
|
||||||
keys = '',
|
keys = '',
|
||||||
keysEventRegExp = new RegExp(
|
keysEventRegExp = new RegExp(
|
||||||
|
@ -21,7 +21,8 @@
|
||||||
function bind(options) {
|
function bind(options) {
|
||||||
var args = Ox.slice(arguments, 1),
|
var args = Ox.slice(arguments, 1),
|
||||||
callbacks = options.callbacks,
|
callbacks = options.callbacks,
|
||||||
that = this;
|
that = this,
|
||||||
|
oxid = that.oxid || 0;
|
||||||
Ox.forEach(
|
Ox.forEach(
|
||||||
Ox.isFunction(args[0]) ? {'*': args[0]} : Ox.makeObject(args),
|
Ox.isFunction(args[0]) ? {'*': args[0]} : Ox.makeObject(args),
|
||||||
function(originalCallback, event) {
|
function(originalCallback, event) {
|
||||||
|
@ -36,7 +37,9 @@
|
||||||
: originalCallback
|
: originalCallback
|
||||||
);
|
);
|
||||||
if (isKeyboardEvent(event)) {
|
if (isKeyboardEvent(event)) {
|
||||||
hasCallback[event] = true;
|
keyboardCallbacks[oxid] = (
|
||||||
|
keyboardCallbacks[oxid] || []
|
||||||
|
).concat(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -72,10 +75,8 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
function onKeydown(e) {
|
function onKeydown(e) {
|
||||||
if (Ox.Focus.focusedElementIsInput()) {
|
var $element = Ox.Focus.focusedElement() || Ox.$body,
|
||||||
return;
|
isInput = Ox.Focus.focusedElementIsInput(),
|
||||||
}
|
|
||||||
var $element = Ox.Focus.focusedElement(),
|
|
||||||
keyName = Ox.KEYS[e.keyCode],
|
keyName = Ox.KEYS[e.keyCode],
|
||||||
keyBasename = keyName.split('.')[0],
|
keyBasename = keyName.split('.')[0],
|
||||||
key = Object.keys(Ox.MODIFIER_KEYS).filter(function(key) {
|
key = Object.keys(Ox.MODIFIER_KEYS).filter(function(key) {
|
||||||
|
@ -85,35 +86,39 @@
|
||||||
}).concat(keyName).join('_'),
|
}).concat(keyName).join('_'),
|
||||||
event = 'key.' + key,
|
event = 'key.' + key,
|
||||||
triggerEvent = function() {
|
triggerEvent = function() {
|
||||||
if ($element) {
|
if (Ox.Focus.focusedElement()) {
|
||||||
$element.triggerEvent.apply($element, arguments);
|
$element.triggerEvent.apply($element, arguments);
|
||||||
} else {
|
} else if (!isInput) {
|
||||||
Ox.Event.trigger.apply(
|
Ox.Event.trigger.apply(
|
||||||
Ox.$body, [{}].concat(Ox.slice(arguments))
|
$element, [{}].concat(Ox.slice(arguments))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
triggerEvent(event, e);
|
triggerEvent(event, e);
|
||||||
if (isKeysEventKey(key)) {
|
if (!isInput) {
|
||||||
// don't register leading spaces or trailing double spaces
|
if (isKeysEventKey(key)) {
|
||||||
if (keyName != 'space' || (
|
// don't register leading spaces or trailing double spaces
|
||||||
keys != '' && !Ox.endsWith(keys, ' ')
|
if (keyName != 'space' || (
|
||||||
)) {
|
keys != '' && !Ox.endsWith(keys, ' ')
|
||||||
keys += chars[keyName] || keyBasename;
|
)) {
|
||||||
// clear the trigger timeout only if the key registered
|
keys += chars[keyName] || keyBasename;
|
||||||
clearTimeout(triggerTimeout);
|
// clear the trigger timeout only if the key registered
|
||||||
triggerTimeout = setTimeout(function() {
|
clearTimeout(triggerTimeout);
|
||||||
triggerEvent('keys', Ox.extend(e, {keys: keys}));
|
triggerTimeout = setTimeout(function() {
|
||||||
}, 250);
|
triggerEvent('keys', Ox.extend(e, {keys: keys}));
|
||||||
|
}, 250);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// clear the reset timeout even if the key didn't register
|
||||||
|
clearTimeout(resetTimeout);
|
||||||
|
resetTimeout = setTimeout(function() {
|
||||||
|
keys = '';
|
||||||
|
}, 1000);
|
||||||
|
if (
|
||||||
|
Ox.contains(keyboardCallbacks[$element.oxid || 0] || [], event)
|
||||||
|
) {
|
||||||
|
e.preventDefault();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// clear the reset timeout even if the key didn't register
|
|
||||||
clearTimeout(resetTimeout);
|
|
||||||
resetTimeout = setTimeout(function() {
|
|
||||||
keys = '';
|
|
||||||
}, 1000);
|
|
||||||
if (hasCallback[event]) {
|
|
||||||
e.preventDefault();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,7 +143,8 @@
|
||||||
|
|
||||||
function unbind(options) {
|
function unbind(options) {
|
||||||
var args = Ox.slice(arguments, 1),
|
var args = Ox.slice(arguments, 1),
|
||||||
callbacks = options.callbacks;
|
callbacks = options.callbacks,
|
||||||
|
oxid = this.oxid || 0;
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
// unbind all handlers for all events
|
// unbind all handlers for all events
|
||||||
callbacks = [];
|
callbacks = [];
|
||||||
|
@ -161,8 +167,14 @@
|
||||||
delete callbacks[event];
|
delete callbacks[event];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isKeyboardEvent(event) && !callbacks[event]) {
|
if (isKeyboardEvent(event)) {
|
||||||
delete hasCallback[event];
|
var index = keyboardCallbacks[oxid].indexOf(event);
|
||||||
|
keyboardCallbacks[oxid].splice(
|
||||||
|
keyboardCallbacks[oxid].indexOf(event), 1
|
||||||
|
)
|
||||||
|
if (keyboardCallbacks[oxid].length == 0) {
|
||||||
|
delete keyboardCallbacks[oxid];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue