From 508ac91b824c11f54a95246809416d2345b33841 Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Tue, 22 May 2018 17:24:22 -0400 Subject: [PATCH] Runtime emits key pressed event --- src/blocks/scratch3_event.js | 9 +++++++++ src/io/keyboard.js | 8 +------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/blocks/scratch3_event.js b/src/blocks/scratch3_event.js index 1964ccf9f..ba496a172 100644 --- a/src/blocks/scratch3_event.js +++ b/src/blocks/scratch3_event.js @@ -7,6 +7,15 @@ class Scratch3EventBlocks { * @type {Runtime} */ this.runtime = runtime; + + this.runtime.on('KEY_PRESSED', key => { + this.runtime.startHats('event_whenkeypressed', { + KEY_OPTION: key + }); + this.runtime.startHats('event_whenkeypressed', { + KEY_OPTION: 'any' + }); + }); } /** diff --git a/src/io/keyboard.js b/src/io/keyboard.js index 7feb6fecc..b1183c5f4 100644 --- a/src/io/keyboard.js +++ b/src/io/keyboard.js @@ -116,17 +116,11 @@ class Keyboard { if (scratchKey === '') return; const index = this._keysPressed.indexOf(scratchKey); if (data.isDown) { + this.runtime.emit('KEY_PRESSED', scratchKey); // If not already present, add to the list. if (index < 0) { this._keysPressed.push(scratchKey); } - // Always trigger hats, even if it was already pressed. - this.runtime.startHats('event_whenkeypressed', { - KEY_OPTION: scratchKey - }); - this.runtime.startHats('event_whenkeypressed', { - KEY_OPTION: 'any' - }); } else if (index > -1) { // If already present, remove from the list. this._keysPressed.splice(index, 1);