Merge pull request #1162 from ericrosenbaum/feature/emit-keyboard-events

Runtime emits key pressed event
This commit is contained in:
Eric Rosenbaum 2018-05-23 17:04:05 -04:00 committed by GitHub
commit b052205457
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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);