From 1f750eb56047320f5c835c8b997f75e5e513d4eb Mon Sep 17 00:00:00 2001 From: Gubolin Date: Mon, 3 Nov 2014 16:32:17 +0100 Subject: add ANY KEY to key hat block (fix #387) --- objects.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'objects.js') diff --git a/objects.js b/objects.js index 4be213a..ecde750 100644 --- a/objects.js +++ b/objects.js @@ -3483,7 +3483,15 @@ SpriteMorph.prototype.allHatBlocksForKey = function (key) { return this.scripts.children.filter(function (morph) { if (morph.selector) { if (morph.selector === 'receiveKey') { - return morph.inputs()[0].evaluate()[0] === key; + var selectedOption = morph.inputs()[0].evaluate()[0]; + + if (selectedOption === 'any key') { + return true; + } + if (selectedOption === key) { + return true; + } + return false; } } return false; -- cgit v1.3.1 From cb4302b455a0ace2d79bd3090f32d913e8ac6612 Mon Sep 17 00:00:00 2001 From: Gubolin Date: Mon, 3 Nov 2014 16:32:57 +0100 Subject: add NUMBER KEY to key hat block --- blocks.js | 1 + objects.js | 4 ++++ 2 files changed, 5 insertions(+) (limited to 'objects.js') diff --git a/blocks.js b/blocks.js index 381c5a3..959948e 100644 --- a/blocks.js +++ b/blocks.js @@ -954,6 +954,7 @@ SyntaxElementMorph.prototype.labelPart = function (spec) { false, { 'any key': ['any key'], + 'number key': ['number key'], 'up arrow': ['up arrow'], 'down arrow': ['down arrow'], 'right arrow': ['right arrow'], diff --git a/objects.js b/objects.js index ecde750..2263883 100644 --- a/objects.js +++ b/objects.js @@ -3488,6 +3488,10 @@ SpriteMorph.prototype.allHatBlocksForKey = function (key) { if (selectedOption === 'any key') { return true; } + if (selectedOption === 'number key' && + (key >= '0' && key <= '9')) { + return true; + } if (selectedOption === key) { return true; } -- cgit v1.3.1 From 5b9385d4644a11374b3c7899f9f6e2092ca92c2f Mon Sep 17 00:00:00 2001 From: Gubolin Date: Mon, 3 Nov 2014 16:57:06 +0100 Subject: add global LAST KEY PRESSED variable --- objects.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'objects.js') diff --git a/objects.js b/objects.js index 2263883..d0d48e3 100644 --- a/objects.js +++ b/objects.js @@ -581,6 +581,11 @@ SpriteMorph.prototype.initBlocks = function () { category: 'control', spec: 'when %keyHat key pressed' }, + getLastKey: { + type: 'reporter', + category: 'control', + spec: 'last key pressed' + }, receiveClick: { type: 'hat', category: 'control', @@ -1801,6 +1806,8 @@ SpriteMorph.prototype.blockTemplates = function (category) { blocks.push(block('receiveGo')); blocks.push(block('receiveKey')); + blocks.push(watcherToggle('getLastKey')); + blocks.push(block('getLastKey')); blocks.push(block('receiveClick')); blocks.push(block('receiveMessage')); blocks.push('-'); @@ -3539,6 +3546,16 @@ SpriteMorph.prototype.getTempo = function () { return 0; }; +// SpriteMorph last key + +SpriteMorph.prototype.getLastKey = function () { + var stage = this.parentThatIsA(StageMorph); + if (stage) { + return stage.getLastKey(); + } + return ''; +}; + // SpriteMorph last message SpriteMorph.prototype.getLastMessage = function () { @@ -4259,6 +4276,7 @@ StageMorph.prototype.init = function (globals) { this.timerStart = Date.now(); this.tempo = 60; // bpm + this.lastKey = ''; this.lastMessage = ''; this.watcherUpdateFrequency = 2; @@ -4539,6 +4557,12 @@ StageMorph.prototype.getTempo = function () { return +this.tempo; }; +// StageMorph keys + +StageMorph.prototype.getLastKey = function () { + return this.lastKey || ''; +}; + // StageMorph messages StageMorph.prototype.getLastMessage = function () { @@ -4733,6 +4757,7 @@ StageMorph.prototype.fireKeyEvent = function (key) { if (evt === 'esc') { return this.fireStopAllEvent(); } + this.lastKey = evt; this.children.concat(this).forEach(function (morph) { if (morph instanceof SpriteMorph || morph instanceof StageMorph) { hats = hats.concat(morph.allHatBlocksForKey(evt)); @@ -4951,6 +4976,8 @@ StageMorph.prototype.blockTemplates = function (category) { blocks.push(block('receiveGo')); blocks.push(block('receiveKey')); + blocks.push(watcherToggle('getLastKey')); + blocks.push(block('getLastKey')); blocks.push(block('receiveClick')); blocks.push(block('receiveMessage')); blocks.push('-'); @@ -6742,7 +6769,7 @@ WatcherMorph.prototype.object = function () { WatcherMorph.prototype.isGlobal = function (selector) { return contains( - ['getLastAnswer', 'getLastMessage', 'getTempo', 'getTimer', + ['getLastAnswer', 'getLastKey', 'getLastMessage', 'getTempo', 'getTimer', 'reportMouseX', 'reportMouseY'], selector ); -- cgit v1.3.1 From b5c611242ed59dc8137e8157e922603ab57bf969 Mon Sep 17 00:00:00 2001 From: Gubolin Date: Tue, 4 Nov 2014 10:57:59 +0100 Subject: remove global LAST KEY and add a sensing reporter instead --- objects.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'objects.js') diff --git a/objects.js b/objects.js index d0d48e3..8f4132e 100644 --- a/objects.js +++ b/objects.js @@ -581,11 +581,6 @@ SpriteMorph.prototype.initBlocks = function () { category: 'control', spec: 'when %keyHat key pressed' }, - getLastKey: { - type: 'reporter', - category: 'control', - spec: 'last key pressed' - }, receiveClick: { type: 'hat', category: 'control', @@ -835,6 +830,11 @@ SpriteMorph.prototype.initBlocks = function () { category: 'sensing', spec: 'key %key pressed?' }, + getKeysPressed: { + type: 'reporter', + category: 'sensing', + spec: 'keys pressed' + }, reportDistanceTo: { type: 'reporter', category: 'sensing', @@ -1806,8 +1806,6 @@ SpriteMorph.prototype.blockTemplates = function (category) { blocks.push(block('receiveGo')); blocks.push(block('receiveKey')); - blocks.push(watcherToggle('getLastKey')); - blocks.push(block('getLastKey')); blocks.push(block('receiveClick')); blocks.push(block('receiveMessage')); blocks.push('-'); @@ -1876,6 +1874,7 @@ SpriteMorph.prototype.blockTemplates = function (category) { blocks.push(block('reportMouseDown')); blocks.push('-'); blocks.push(block('reportKeyPressed')); + blocks.push(block('getKeysPressed')); blocks.push('-'); blocks.push(block('reportDistanceTo')); blocks.push('-'); @@ -3548,10 +3547,10 @@ SpriteMorph.prototype.getTempo = function () { // SpriteMorph last key -SpriteMorph.prototype.getLastKey = function () { +SpriteMorph.prototype.getKeysPressed = function () { var stage = this.parentThatIsA(StageMorph); if (stage) { - return stage.getLastKey(); + return stage.getKeysPressed(); } return ''; }; @@ -4276,7 +4275,6 @@ StageMorph.prototype.init = function (globals) { this.timerStart = Date.now(); this.tempo = 60; // bpm - this.lastKey = ''; this.lastMessage = ''; this.watcherUpdateFrequency = 2; @@ -4559,8 +4557,12 @@ StageMorph.prototype.getTempo = function () { // StageMorph keys -StageMorph.prototype.getLastKey = function () { - return this.lastKey || ''; +StageMorph.prototype.getKeysPressed = function () { + var keys = []; + for (var key in this.keysPressed) { + keys.push(key); + } + return new List(keys); }; // StageMorph messages @@ -4757,7 +4759,6 @@ StageMorph.prototype.fireKeyEvent = function (key) { if (evt === 'esc') { return this.fireStopAllEvent(); } - this.lastKey = evt; this.children.concat(this).forEach(function (morph) { if (morph instanceof SpriteMorph || morph instanceof StageMorph) { hats = hats.concat(morph.allHatBlocksForKey(evt)); @@ -4976,8 +4977,6 @@ StageMorph.prototype.blockTemplates = function (category) { blocks.push(block('receiveGo')); blocks.push(block('receiveKey')); - blocks.push(watcherToggle('getLastKey')); - blocks.push(block('getLastKey')); blocks.push(block('receiveClick')); blocks.push(block('receiveMessage')); blocks.push('-'); @@ -5040,6 +5039,7 @@ StageMorph.prototype.blockTemplates = function (category) { blocks.push(block('reportMouseDown')); blocks.push('-'); blocks.push(block('reportKeyPressed')); + blocks.push(block('getKeysPressed')); blocks.push('-'); blocks.push(block('doResetTimer')); blocks.push(watcherToggle('getTimer')); @@ -6769,7 +6769,7 @@ WatcherMorph.prototype.object = function () { WatcherMorph.prototype.isGlobal = function (selector) { return contains( - ['getLastAnswer', 'getLastKey', 'getLastMessage', 'getTempo', 'getTimer', + ['getLastAnswer', 'getKeysPressed', 'getLastMessage', 'getTempo', 'getTimer', 'reportMouseX', 'reportMouseY'], selector ); -- cgit v1.3.1 From 98755a4467991b6fb31563b17f8c15b1c5266294 Mon Sep 17 00:00:00 2001 From: Gubolin Date: Tue, 4 Nov 2014 10:58:10 +0100 Subject: fix typo --- objects.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'objects.js') diff --git a/objects.js b/objects.js index 8f4132e..db72b9c 100644 --- a/objects.js +++ b/objects.js @@ -4618,7 +4618,7 @@ StageMorph.prototype.step = function () { world.keyboardReceiver = this; } if (world.currentKey === null) { - this.keyPressed = null; + this.keysPressed = {}; } // manage threads -- cgit v1.3.1