mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-08 22:01:58 -05:00
Simplify Key a bit.
This commit is contained in:
parent
2e43fb0997
commit
95bfebf64b
1 changed files with 35 additions and 25 deletions
|
@ -3,6 +3,10 @@ var Key = new function() {
|
||||||
var keys = {
|
var keys = {
|
||||||
'8': 'backspace',
|
'8': 'backspace',
|
||||||
'13': 'enter',
|
'13': 'enter',
|
||||||
|
'16': 'shift',
|
||||||
|
'17': 'control',
|
||||||
|
'19': 'alt',
|
||||||
|
'20': 'capsLock',
|
||||||
'27': 'escape',
|
'27': 'escape',
|
||||||
'32': 'space',
|
'32': 'space',
|
||||||
'37': 'left',
|
'37': 'left',
|
||||||
|
@ -10,40 +14,47 @@ var Key = new function() {
|
||||||
'39': 'right',
|
'39': 'right',
|
||||||
'40': 'down',
|
'40': 'down',
|
||||||
'46': 'delete',
|
'46': 'delete',
|
||||||
|
'91': 'command'
|
||||||
};
|
};
|
||||||
var modifierNames = [
|
|
||||||
'shift', 'shiftKey',
|
|
||||||
'control', 'ctrlKey',
|
|
||||||
'alt', 'altKey',
|
|
||||||
'command', 'metaKey'
|
|
||||||
// TODO: capslock
|
|
||||||
];
|
|
||||||
var activeKeys = {};
|
var activeKeys = {};
|
||||||
|
|
||||||
|
var modifiers = this.modifiers = {
|
||||||
|
shift: false,
|
||||||
|
control: false,
|
||||||
|
alt: false,
|
||||||
|
command: false,
|
||||||
|
capsLock: false
|
||||||
|
};
|
||||||
|
|
||||||
|
function setActive(key, active) {
|
||||||
|
var keyIsActive = activeKeys[key];
|
||||||
|
if (!keyIsActive) {
|
||||||
|
if(active)
|
||||||
|
activeKeys[key] = true;
|
||||||
|
} else {
|
||||||
|
delete activeKeys[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var eventHandlers = Base.each(['keyDown', 'keyUp'], function(type) {
|
var eventHandlers = Base.each(['keyDown', 'keyUp'], function(type) {
|
||||||
var toolHandler = 'on' + Base.capitalize(type),
|
var toolHandler = 'on' + Base.capitalize(type),
|
||||||
down = type == 'keyDown';
|
keyDown = type == 'keyDown';
|
||||||
this[type.toLowerCase()] = function(event) {
|
this[type.toLowerCase()] = function(event) {
|
||||||
|
console.log(event.which || event.keyCode);
|
||||||
var code = event.which || event.keyCode,
|
var code = event.which || event.keyCode,
|
||||||
key = keys[code] || String.fromCharCode(code).toLowerCase(),
|
key = keys[code] || String.fromCharCode(code).toLowerCase();
|
||||||
modifiers = {};
|
// Activate or deactivate the key:
|
||||||
activeKeys[key] = down;
|
setActive(key, keyDown);
|
||||||
// Add the modifier keys to or remove from the activeKeys
|
|
||||||
// and construct the modifiers object to be passed to the
|
|
||||||
// key event
|
|
||||||
for (var i = 0, l = modifierNames.length; i < l; i += 2) {
|
|
||||||
var modifierKey = modifierNames[i];
|
|
||||||
if ((down && event[modifierNames[i + 1]])
|
|
||||||
|| (!down && modifierKey)) {
|
|
||||||
activeKeys[modifierKey] = down;
|
|
||||||
}
|
|
||||||
modifiers[modifierKey] = down;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// If the key is a modifier, update the modifiers:
|
||||||
|
if (modifiers[key] !== undefined)
|
||||||
|
modifiers[key] = keyDown;
|
||||||
|
|
||||||
|
// Call the onKeyDown or onKeyUp handler if present:
|
||||||
if(paper.tool[toolHandler]) {
|
if(paper.tool[toolHandler]) {
|
||||||
paper.tool[toolHandler]({
|
paper.tool[toolHandler]({
|
||||||
type: down ? 'key-down' : 'key-up',
|
type: keyDown ? 'key-down' : 'key-up',
|
||||||
keyCode: code,
|
keyCode: code,
|
||||||
character: key,
|
character: key,
|
||||||
modifiers: modifiers
|
modifiers: modifiers
|
||||||
|
@ -51,7 +62,6 @@ var Key = new function() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
Event.add(document, eventHandlers);
|
Event.add(document, eventHandlers);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
Loading…
Reference in a new issue