More hats ()

* Key pressed hat

* Tabs -> Spaces

* Oops

* Edge activate

* Fix hat

Phew...

* I forgot to change it also here

:/ :/ :\

* Minor fixes for TravisCi

* Minor docs

* Line length
This commit is contained in:
dekrain 2016-09-03 22:33:45 +02:00 committed by Tim Mickel
parent 2b84c8d0fe
commit 7caf8e588a
3 changed files with 35 additions and 3 deletions

View file

@ -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
},*/

View file

@ -54,7 +54,7 @@ function Runtime () {
this.ioDevices = {
'clock': new Clock(),
'keyboard': new Keyboard(),
'keyboard': new Keyboard(this),
'mouse': new Mouse()
};
}

View file

@ -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.