Skip to content

Commit 4187f55

Browse files
committed
Record which keys the project has used
Related to TurboWarp/scratch-gui#1106
1 parent 92adc76 commit 4187f55

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/io/keyboard.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ class Keyboard {
5353
// tw: track last pressed key
5454
this.lastKeyPressed = '';
5555
this._numeralKeyCodesToStringKey = new Map();
56+
/**
57+
* Set of Scratch keys used by the project.
58+
*/
59+
this._usedKeys = new Set();
5660
}
5761

5862
/**
@@ -198,13 +202,22 @@ class Keyboard {
198202
return this._keysPressed.length > 0;
199203
}
200204
const scratchKey = this._keyArgToScratchKey(keyArg);
205+
this._usedKeys.add(scratchKey);
201206
return this._keysPressed.indexOf(scratchKey) > -1;
202207
}
203208

204209
// tw: expose last pressed key
205210
getLastKeyPressed () {
206211
return this.lastKeyPressed;
207212
}
213+
214+
/**
215+
* @param {string} scratchKey Scratch key
216+
* @returns {boolean} true if the project has used this key
217+
*/
218+
hasUsedKey (scratchKey) {
219+
return this._usedKeys.has(scratchKey);
220+
}
208221
}
209222

210223
module.exports = Keyboard;

test/unit/io_keyboard_tw.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,21 @@ test('holding shift and key, releasing shift, waiting, then releasing key', t =>
110110

111111
t.end();
112112
});
113+
114+
test('hasUsedKey', t => {
115+
const rt = new Runtime();
116+
const k = new Keyboard(rt);
117+
118+
t.equal(k.hasUsedKey('backspace'), false);
119+
t.equal(k.hasUsedKey('delete'), false);
120+
121+
k.getKeyIsDown('backspace');
122+
t.equal(k.hasUsedKey('backspace'), true);
123+
t.equal(k.hasUsedKey('delete'), false);
124+
125+
k.getKeyIsDown('delete');
126+
t.equal(k.hasUsedKey('backspace'), true);
127+
t.equal(k.hasUsedKey('delete'), true);
128+
129+
t.end();
130+
});

0 commit comments

Comments
 (0)