Runtime emits key pressed event

This commit is contained in:
Eric Rosenbaum 2018-05-22 17:24:22 -04:00
parent b7e40c9735
commit 508ac91b82
2 changed files with 10 additions and 7 deletions

View file

@ -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'
});
});
}
/**

View file

@ -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);