Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Resolves
Resolves #1892.
See prior art & discussion in #2247.
Proposed Changes
Adds support for detecting the shift and backspace keys in the "key pressed?" block.
User-apparent details:
Internal details:
KEY_NAMES_EXCLUDED_FOR_KEY_ANY_PRESSED
array is added (its only item is'shift'
). Scratch keys here are not counted for the "key any pressed?" or "when any key pressed" blocks.getKeyIsDown
now performs a.some()
call to check if any of the keys currently pressed is not excluded. This will have only minuscule performance overhead - in all cases it will do one of the following:.some()
on an empty array is always false.).some()
exits as soon as it matches one true value.)'shift'
. (.some()
returns false once it's tested every item and none of them was true.)shift
and is followed by a valid key.postData
now usually emits two events - the existing one remainsemit('KEY_PRESSED', scratchKey)
and the new one isemit('KEY_ANY_PRESSED')
. The former is used for responding to specific keys; the latter for responding to "any" key (in which case it doesn't matter which key is pressed, and so that detail is not emitted).postData
isn't called at nearly a high enough frequency for this to be costly.scratch3_event.js
is updated to listen to both of these events: the first for starting specific-key "when key pressed" blocks, the second for "when any key pressed" blocks.scratch3_sensing.js
requires no changes because it already callsioQuery('keyboard', 'getKeyIsDown', ['any'])
.)Reason for Changes
Test Coverage
Tested manually and with unit tests.
Use the script / project linked: Keyboard Project.zip
For unit tests:
KEY_PRESSED
andKEY_ANY_PRESSED
events used by the "when key pressed" block, improving test coverage (and verifying an important detail that "shift" shouldn't count for "when any key pressed").