Files
WebODF/webodf/lib/gui/KeyboardHandler.js
T

192 lines
4.9 KiB
JavaScript
Raw Normal View History

2013-07-11 20:28:10 +10:00
/**
* @license
* Copyright (C) 2012-2013 KO GmbH <copyright@kogmbh.com>
*
* @licstart
* The JavaScript code in this page is free software: you can redistribute it
* and/or modify it under the terms of the GNU Affero General Public License
* (GNU AGPL) as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. The code is distributed
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU AGPL for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this code. If not, see <http://www.gnu.org/licenses/>.
*
2013-07-11 20:28:10 +10:00
* As additional permission under GNU AGPL version 3 section 7, you
* may distribute non-source (e.g., minimized or compacted) forms of
* that code without the copy of the GNU GPL normally required by
* section 4, provided you include this license notice and a URL
* through which recipients can access the Corresponding Source.
*
* As a special exception to the AGPL, any HTML file which merely makes function
* calls to this code, and for that purpose includes it by reference shall be
* deemed a separate work for copyright law purposes. In addition, the copyright
* holders of this code give you permission to combine this code with free
* software libraries that are released under the GNU LGPL. You may copy and
* distribute such a system following the terms of the GNU AGPL for this code
* and the LGPL for the libraries. If you modify this code, you may extend this
* exception to your version of the code, but you are not obligated to do so.
* If you do not wish to do so, delete this exception statement from your
* version.
*
* This license applies to this entire compilation.
* @licend
* @source: http://www.webodf.org/
* @source: https://github.com/kogmbh/WebODF/
2013-07-11 20:28:10 +10:00
*/
/*global gui, runtime */
/*jslint bitwise: true*/
2013-07-11 20:28:10 +10:00
/**
* @constructor
* @return {?}
*/
gui.KeyboardHandler = function KeyboardHandler() {
"use strict";
var modifier = gui.KeyboardHandler.Modifier,
defaultBinding = null,
bindings = {};
/**
2013-07-11 20:28:10 +10:00
* @return {!number}
*/
function getModifiers(e) {
2013-07-11 20:28:10 +10:00
var modifiers = modifier.None;
if (e.metaKey) { modifiers |= modifier.Meta; }
if (e.ctrlKey) { modifiers |= modifier.Ctrl; }
if (e.altKey) { modifiers |= modifier.Alt; }
if (e.shiftKey) { modifiers |= modifier.Shift; }
return modifiers;
}
/**
2013-07-11 20:28:10 +10:00
* @return {!string}
*/
function getKeyCombo(keyCode, modifiers) {
2013-07-11 20:28:10 +10:00
if (!modifiers) {
modifiers = modifier.None;
2013-07-11 20:28:10 +10:00
}
return keyCode + ':' + modifiers;
}
/**
2013-07-11 20:28:10 +10:00
* @param {!Function} callback
*/
this.setDefault = function (callback) {
defaultBinding = callback;
};
/**
* @param {!number} keyCode
* @param {!number} modifiers
* @param {!Function} callback
*/
this.bind = function (keyCode, modifiers, callback) {
2013-07-11 20:28:10 +10:00
var keyCombo = getKeyCombo(keyCode, modifiers);
runtime.assert(bindings.hasOwnProperty(keyCombo) === false,
"tried to overwrite the callback handler of key combo: " + keyCombo);
bindings[keyCombo] = callback;
};
/**
* @param {!number} keyCode
* @@param {!number} modifiers
*/
this.unbind = function (keyCode, modifiers) {
2013-07-11 20:28:10 +10:00
var keyCombo = getKeyCombo(keyCode, modifiers);
delete bindings[keyCombo];
};
/*
* removes all the bindings includes the default binding
*/
this.reset = function () {
defaultBinding = null;
bindings = {};
};
/**
* @param {!Event} e
*/
this.handleEvent = function (e) {
var keyCombo = getKeyCombo(e.keyCode, getModifiers(e)),
callback = bindings[keyCombo],
handled = false;
if (callback) {
handled = callback();
} else if (defaultBinding !== null) {
handled = defaultBinding(e);
}
if (handled) {
if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
}
};
};
/**@const*/
gui.KeyboardHandler.Modifier = {
2013-07-11 20:28:10 +10:00
None: 0,
Meta: 1,
Ctrl: 2,
Alt: 4,
CtrlAlt: 6,
2013-07-11 20:28:10 +10:00
Shift: 8,
MetaShift: 9,
CtrlShift: 10,
AltShift: 12
};
/**@const*/
gui.KeyboardHandler.KeyCode = {
2013-07-11 20:28:10 +10:00
Backspace: 8,
2013-07-24 15:55:13 +10:00
Tab: 9,
2013-07-30 14:49:14 +10:00
Clear: 12,
2013-07-11 20:28:10 +10:00
Enter: 13,
End: 35,
Home: 36,
Left: 37,
Up: 38,
Right: 39,
Down: 40,
Delete: 46,
A: 65,
B: 66,
2013-09-06 03:41:56 +10:00
C: 67,
D: 68,
E: 69,
F: 70,
G: 71,
H: 72,
I: 73,
2013-09-06 03:41:56 +10:00
J: 74,
K: 75,
L: 76,
M: 77,
N: 78,
O: 79,
P: 80,
Q: 81,
R: 82,
S: 83,
T: 84,
U: 85,
2013-09-06 03:41:56 +10:00
V: 86,
W: 87,
X: 88,
Y: 89,
2013-07-11 20:28:10 +10:00
Z: 90
};
(function () {
"use strict";
return gui.KeyboardHandler;
}());