mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-22 07:19:57 -05:00
Implement onKeyDown / onKeyUp & Key#isDown(key).
This commit is contained in:
parent
1e4ae9afb4
commit
05a138bc7b
4 changed files with 67 additions and 3 deletions
|
@ -69,7 +69,9 @@ var sources = [
|
|||
'src/util/Element.js',
|
||||
'src/util/Event.js',
|
||||
'src/util/Numerical.js',
|
||||
'src/util/PaperScript.js'
|
||||
'src/util/PaperScript.js',
|
||||
|
||||
'src/ui/Key.js'
|
||||
];
|
||||
|
||||
// Load unit tests after library if asked to do so
|
||||
|
|
|
@ -118,4 +118,5 @@ Base.inject({
|
|||
//#include "util/Numerical.js"
|
||||
//#include "util/PaperScript.js"
|
||||
|
||||
//#include "ui/Key.js"
|
||||
};
|
||||
|
|
61
src/ui/Key.js
Normal file
61
src/ui/Key.js
Normal file
|
@ -0,0 +1,61 @@
|
|||
var Key = new function() {
|
||||
// TODO: make sure the keys are called the same in Scriptographer
|
||||
var keys = {
|
||||
'8': 'backspace',
|
||||
'13': 'enter',
|
||||
'27': 'escape',
|
||||
'32': 'space',
|
||||
'37': 'left',
|
||||
'38': 'up',
|
||||
'39': 'right',
|
||||
'40': 'down',
|
||||
'46': 'delete',
|
||||
|
||||
};
|
||||
var modifierNames = [
|
||||
'shift', 'shiftKey',
|
||||
'control', 'ctrlKey',
|
||||
'alt', 'altKey',
|
||||
'command', 'metaKey'
|
||||
// TODO: capslock
|
||||
];
|
||||
var activeKeys = {};
|
||||
|
||||
var eventHandlers = Base.each(['keyDown', 'keyUp'], function(type) {
|
||||
var toolHandler = 'on' + Base.capitalize(type),
|
||||
down = type == 'keyDown';
|
||||
this[type.toLowerCase()] = function(event) {
|
||||
var code = event.which || event.keyCode,
|
||||
key = keys[code] || String.fromCharCode(code).toLowerCase(),
|
||||
modifiers = {};
|
||||
activeKeys[key] = down;
|
||||
// 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(paper.tool[toolHandler]) {
|
||||
paper.tool[toolHandler]({
|
||||
keyCode: code,
|
||||
character: key,
|
||||
modifiers: modifiers
|
||||
});
|
||||
}
|
||||
}
|
||||
}, {});
|
||||
|
||||
Event.add(document, eventHandlers);
|
||||
|
||||
return {
|
||||
isDown: function(key) {
|
||||
return !!activeKeys[key];
|
||||
}
|
||||
};
|
||||
};
|
|
@ -131,13 +131,13 @@ var PaperScript = new function() {
|
|||
|
||||
function run(code) {
|
||||
with (paper) {
|
||||
paper.tool = /onMouse(?:Up|Down|Move|Drag)/.test(code) && new Tool();
|
||||
paper.tool = /on(?:Key|Mouse)(?:Up|Down|Move|Drag)/.test(code) && new Tool();
|
||||
var res = eval(compile(code)),
|
||||
doc = paper.document;
|
||||
if (paper.tool) {
|
||||
Base.each(['onEditOptions', 'onOptions', 'onSelect',
|
||||
'onDeselect', 'onReselect', 'onMouseDown', 'onMouseUp',
|
||||
'onMouseDrag', 'onMouseMove'], function(key) {
|
||||
'onMouseDrag', 'onMouseMove', 'onKeyDown', 'onKeyUp'], function(key) {
|
||||
try {
|
||||
paper.tool[key] = eval(key);
|
||||
} catch (e) {
|
||||
|
|
Loading…
Reference in a new issue