diff --git a/src/blocks/scratch3_event.js b/src/blocks/scratch3_event.js index 71326d494..900fd1f2a 100644 --- a/src/blocks/scratch3_event.js +++ b/src/blocks/scratch3_event.js @@ -23,12 +23,13 @@ Scratch3EventBlocks.prototype.getHats = function () { 'event_whenflagclicked': { restartExistingThreads: true }, - /*'event_whenkeypressed': { + 'event_whenkeypressed': { restartExistingThreads: false }, 'event_whenthisspriteclicked': { restartExistingThreads: true }, + /* 'event_whenbackdropswitchesto': { restartExistingThreads: true },*/ diff --git a/src/engine/runtime.js b/src/engine/runtime.js index fe539dfcd..db157783b 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -54,7 +54,7 @@ function Runtime () { this.ioDevices = { 'clock': new Clock(), - 'keyboard': new Keyboard(), + 'keyboard': new Keyboard(this), 'mouse': new Mouse() }; } diff --git a/src/io/keyboard.js b/src/io/keyboard.js index b3a08bae6..dc0f2dc72 100644 --- a/src/io/keyboard.js +++ b/src/io/keyboard.js @@ -1,11 +1,17 @@ var Cast = require('../util/cast'); -function Keyboard () { +function Keyboard (runtime) { /** * List of currently pressed keys. * @type{Array.<number>} */ this._keysPressed = []; + /** + * Reference to the owning Runtime. + * Can be used, for example, to activate hats. + * @type{!Runtime} + */ + this.runtime = runtime; } /** @@ -31,6 +37,21 @@ Keyboard.prototype._scratchKeyToKeyCode = function (keyName) { return keyString.toUpperCase().charCodeAt(0); }; +Keyboard.prototype._keyCodeToScratchKey = function (keyCode) { + if (keyCode >= 48 && keyCode <= 90) { + // Standard letter. + return String.fromCharCode(keyCode).toLowerCase(); + } + switch (keyCode) { + case 32: return 'space'; + case 37: return 'leftarrow'; + case 38: return 'uparrow'; + case 39: return 'rightarrow'; + case 40: return 'downarrow'; + } + return null; +}; + Keyboard.prototype.postData = function (data) { if (data.keyCode) { var index = this._keysPressed.indexOf(data.keyCode); @@ -38,6 +59,16 @@ Keyboard.prototype.postData = function (data) { // If not already present, add to the list. if (index < 0) { this._keysPressed.push(data.keyCode); + this.runtime.startHats('event_whenkeypressed', + { + 'KEY_OPTION': this._keyCodeToScratchKey(data.keyCode) + .toUpperCase() + }); + + this.runtime.startHats('event_whenkeypressed', + { + 'KEY_OPTION': 'ANY' + }); } } else if (index > -1) { // If already present, remove from the list.