The “key {something} pressed” event block stops detecting a key held down when a second key is pressed. For example, a script says

when space key pressed
change x by 5

When holding space down, the script will continue to change the sprite's x position by 5. While the spacebar is held down, press any other key, and it will stop the event block from detecting the spacebar. It will resume to detect the key after you release and hold the key down again.

I am using Firefox 22.0. Flash player version is in my signature.


I've noticed this too Windows 7, IE10.


every game has glitches so its simple fix your scripting ok


cheezburger30 wrote:

every game has glitches so its simple fix your scripting ok

1. The project I found this glitch on wasn't a game
2. This happens with every project that uses “key pressed” event blocks


Actually, I take that back. I can recall being able to… Maybe it's your computer? I'll test it later.


this “bug” was also in 1.4. So i tihnk it isn't really a bug, even if it is annoying when you try to make a game with these and notice that it doesn't work. Easy workaround: For ever if key pressed do something.


EDIT: Whoa, this ended up longer than I expected - just read it carefully and it explains why you're seeing this ‘bug’…
EDIT2: Oh, hi @ProdigyZeta7 - this explanation may be a bit too simplistic for you in places - didn't notice it was you! (Your pic has changed.) Still I hope it makes sense of the behaviour you see, and explains why using “when key pressed” like that is not really a sensible way to do it (as well as suggesting a better way to use it).

The “when _ key pressed” event doesn't tell you that a key is being held down, it only tells you when the OS sends a key pressed event.

OK, first of all, to understand what's going on, and why it appears as if “when _ key pressed” can be telling you when a key is held down, we have to understand about “key-repeat”…

If you go to a text box of some kind, or into a text-editor/word-processor window, when you hold down a key what happens is that you get an initial letter appear, then there is a slight pause, and then the letter starts to appear again and again, more quickly. This is known as “key-repeat” - a key starts to ‘repeat’ if it is held down.

The delay before the repeat starts to happen (after the initial press), and the speed that the key continues to repeat after that, are both controlled by various settings on your computer (your login account on there). If you switch off key-repeat completely (easy on Mac & Linux, but a bit of a pain on Windows), then holding down a key will no longer cause a repeat - meaning you only see the letter appear once, when you first press the key.

Now, since the “when _ key pressed” event also responds to the repeats (as well as the initial press), what you will find if you switch off key-repeat is that Scratch will also only ever see the very first press of the key - so you cannot tell that the key is held down in that case, and your game will not work as you want.

That should be enough to tell you that you shouldn't really rely on that to check if a key is held down, since it's all dependent on the way the user has their system set up.

But the reason for the ‘bug’ you've described in your case is to do with the way the OS deals with a key-press while another key is held down (it stops the key-repeat right there and then) - try it in your text-editor/word-processor…

However, you *can* still use “when _ key pressed” reliably in conjunction with a “repeat until not key _ pressed” check. For example, something like this:

when space key pressed
repeat until not key space pressed
do stuff…

An alternative that I think could be quite useful under certain situations, goes something like this:

when space key pressed
set pressingspace to 1
wait until pressingspace<0
wait until not key space pressed
set pressingspace to 0

Now, in your main loop you would have something that checks the value of “pressingspace”, and performs some appropriate action if it's not zero. You also set it to -1 (if it was 1), so that the above gets past the “wait until pressingspace<0” and starts waiting for space to be released.

OK, so why is this useful? The reason is that if you have a game which does lots of work between frames (i.e. iterations of its main loop), and you are only using “if key space pressed” in there, that means it's possible for it to ‘miss’ a tap of the space key, because it happened too quickly, within the time that the loop is doing its heavy work, between a pair of the “if key space pressed” checks.

However, “when space key pressed” will still ‘catch’ this case, because it is responding to the actual space key pressed event that gets queued and sent to Scratch to process whenever it can get around to it (rather than only doing check for the key at the specific point in the script that it gets to the “key space pressed” sensor block).

Whew, that ended up longer than I intended - hope it made some sense!


DadOfMrLog wrote:

Okay, that makes sense. So instead of relying on a “key pressed” event block I should make a separate forever loop with “key pressed” sensing block.

Thanks for explaining that.