szurubooru/public_html/js/Keyboard.js

45 lines
905 B
JavaScript
Raw Normal View History

2014-09-19 17:37:10 +02:00
var App = App || {};
2014-10-26 22:51:48 +01:00
App.Keyboard = function(jQuery, mousetrap) {
2014-09-19 17:37:10 +02:00
var oldStopCallback = mousetrap.stopCallback;
mousetrap.stopCallback = function(e, element, combo, sequence) {
if (combo.indexOf('ctrl') !== -1) {
return false;
}
2014-10-26 22:51:48 +01:00
var $focused = jQuery(':focus').eq(0);
if ($focused.length && $focused.prop('tagName').match(/embed|object/i)) {
return true;
}
return oldStopCallback.apply(mousetrap, arguments);
};
2014-09-19 17:37:10 +02:00
function keyup(key, callback) {
2014-10-18 12:42:29 +02:00
unbind(key);
mousetrap.bind(key, callback, 'keyup');
2014-09-19 17:37:10 +02:00
}
function keydown(key, callback) {
2014-10-18 12:42:29 +02:00
unbind(key);
mousetrap.bind(key, callback);
2014-09-19 17:37:10 +02:00
}
function reset() {
mousetrap.reset();
}
2014-10-05 10:09:02 +02:00
function unbind(key) {
2014-10-18 12:42:29 +02:00
mousetrap.unbind(key, 'keyup');
2014-10-05 10:09:02 +02:00
mousetrap.unbind(key);
}
2014-09-19 17:37:10 +02:00
return {
keydown: keydown,
keyup: keyup,
reset: reset,
2014-10-05 10:09:02 +02:00
unbind: unbind,
2014-09-19 17:37:10 +02:00
};
};
2014-10-26 22:51:48 +01:00
App.DI.register('keyboard', ['jQuery', 'mousetrap'], App.Keyboard);