From e52ed9ae3650021fd12a4c155d09e6943627b24f Mon Sep 17 00:00:00 2001 From: Jonathan Puckey Date: Mon, 25 Apr 2011 18:11:01 +0200 Subject: [PATCH] Key handlers: allow the user to prevent the default behaviour of key events. --- src/ui/Key.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/ui/Key.js b/src/ui/Key.js index fb1b8ab4..c51cf8c2 100644 --- a/src/ui/Key.js +++ b/src/ui/Key.js @@ -42,12 +42,24 @@ var Key = new function() { // Call the onKeyDown or onKeyUp handler if present: // TODO: don't call the key handler if the key is a modifier? if (paper.tool[toolHandler]) { - paper.tool[toolHandler]({ + var preventDefault = paper.tool[toolHandler]({ type: keyDown ? 'key-down' : 'key-up', keyCode: code, character: key, - modifiers: modifiers + modifiers: modifiers, + // 'preventDefault: event.preventDefault' throws + // an error in Safari when called, so we have to wrap + // it into a function. + // TODO: port to Scriptographer. + preventDefault: function() { + event.preventDefault() + } }); + // TODO: port to Scriptographer: + // When the handler function returns false, prevent the + // default behaviour of the key event: + if (preventDefault === false) + event.preventDefault(); } }; }, {});