Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » HELP! How do I get when a key was *just* pressed.
- theoW1200
-
Scratcher
100+ posts
HELP! How do I get when a key was *just* pressed.
Yes, i do know about the
<key [ v] pressed?>block. The only problem is that if the button is down for more than 1 screen refresh then it will still report true. How can I detect when a key was JUST pressed. Thanks.
Last edited by theoW1200 (July 20, 2025 09:56:20)
- deck26
-
Scratcher
1000+ posts
HELP! How do I get when a key was *just* pressed.
So you want to only detect a keypress once and if the key is held down you don't want to react to it? The simplest method is a wait until not key pressed after taking the relevant action.
- theoW1200
-
Scratcher
100+ posts
HELP! How do I get when a key was *just* pressed.
I can't use a wait block, it will pause the whole game
Last edited by theoW1200 (July 20, 2025 10:33:02)
- deck26
-
Scratcher
1000+ posts
HELP! How do I get when a key was *just* pressed.
I can't use a wait block, it will pause the whole gameA wait block only pauses that script.
- theoW1200
-
Scratcher
100+ posts
HELP! How do I get when a key was *just* pressed.
AAAAAAAnd im detecting the keypress in the game loop. which will ppuase the game untill the key is let go of
Last edited by theoW1200 (July 20, 2025 11:11:02)
- deck26
-
Scratcher
1000+ posts
HELP! How do I get when a key was *just* pressed.
AAAAAAAnd im detecting the keypress in the game loop. which will ppuase the game untill the key is let go ofShould still be possible to do something with another script and a variable.
When you detect the key is pressed and your variable is 0 you set the variable to 1 and run a broadcast before running the code that should only run when the key is newly pressed. The broadcast receiver runs a wait block to detect the key being released and resets the variable.
So if the key is pressed the variable is set to 1 which stops that bit of code being run and the rest of the loop still runs. The second script resets the variable when the key is released and you're back to where you were for the next keypress. Just make sure the variable gets initialised before the game loop starts.
- theoW1200
-
Scratcher
100+ posts
HELP! How do I get when a key was *just* pressed.
please just want one script not multiple.
- deck26
-
Scratcher
1000+ posts
HELP! How do I get when a key was *just* pressed.
please just want one script not multiple.So don't bother with the wait until, just check at the end of the loop. If the key is not pressed reset the variable to 0.
- theoW1200
-
Scratcher
100+ posts
HELP! How do I get when a key was *just* pressed.
I dont get it.I just need 1 singular script i can slot into my game loop but this is just too confusing.
- deck26
-
Scratcher
1000+ posts
HELP! How do I get when a key was *just* pressed.
Why is it confusing? You set a variable when you detect the key is pressed and reset it when it is released. So all you need to do is only allow the script to run the ‘key pressed’ code when the variable is 0 AND the key is pressed..
- theoW1200
-
Scratcher
100+ posts
HELP! How do I get when a key was *just* pressed.
i do not understand it just keeps toggling the pressed variable
Last edited by theoW1200 (July 20, 2025 15:04:22)
- Bitebite12
-
Scratcher
1000+ posts
HELP! How do I get when a key was *just* pressed.
forever
if <<key [... v] pressed?> and <(foo) = [0]>> then
...
set [foo v] to [1]
end
if <not<key [... v] pressed?>> then
set [foo v] to [0]
end
This should work.
- 10goto10
-
Scratcher
1000+ posts
HELP! How do I get when a key was *just* pressed.
I made an example yesterday that might be helpful.
It does not have any waiting, runs in a main loop, and does requires that a key stopped being pressed before that key press causes action again.
The custom block should be run without screen refresh.
It's not very clean. Notice that it deletes item 0 from the list several times each frame.
It's also important to get the case right. using “Down Arrow” would fail but “down arrow” works.
But maybe this is something you could try and modify.
update: Didn't see Bitebite12's response until after I had posted mine but now you have two examples to look at.
It does not have any waiting, runs in a main loop, and does requires that a key stopped being pressed before that key press causes action again.
define Quickly check keys that have been pressed
set [i v] to [0]
repeat (length of [Keys to Check v]::data)
change [i v] by (1)
if <<key (item (i) of [Keys to Check v]) pressed?> and <not <[Waiting to not be Pressed v] contains (item (i) of [Keys to Check v])?::data>>> then
add (item (i) of [Keys to Check v]) to [Waiting to not be Pressed v]
turn @turnRight (15) degrees::motion
else
if <not <key (item (i) of [Keys to Check v]) pressed?>> then
delete (item # of (item (i) of [Keys to Check v]) in [Waiting to not be Pressed v]) of [Waiting to not be Pressed v]
end
end
end
when @greenFlag clicked
delete all of [Waiting to not be Pressed v]
delete all of [Keys to Check v]
add [up arrow] to [Keys to Check v]
add [down arrow] to [Keys to Check v]
add [A] to [Keys to Check v]
add [D] to [Keys to Check v]
forever
Quickly check keys that have been pressed::custom
end
The custom block should be run without screen refresh.
It's not very clean. Notice that it deletes item 0 from the list several times each frame.
It's also important to get the case right. using “Down Arrow” would fail but “down arrow” works.
But maybe this is something you could try and modify.
update: Didn't see Bitebite12's response until after I had posted mine but now you have two examples to look at.
Last edited by 10goto10 (July 20, 2025 15:33:53)
- theoW1200
-
Scratcher
100+ posts
HELP! How do I get when a key was *just* pressed.
this works tyforever
if <<key [... v] pressed?> and <(foo) = [0]>> then
...
set [foo v] to [1]
end
if <not<key [... v] pressed?>> then
set [foo v] to [0]
end
This should work.
- theoW1200
-
Scratcher
100+ posts
HELP! How do I get when a key was *just* pressed.
This should be a reporter in scratch 4.0
- lowknope
-
New Scratcher
5 posts
HELP! How do I get when a key was *just* pressed.
This should be a reporter in scratch 4.0boolean*
- Discussion Forums
- » Help with Scripts
-
» HELP! How do I get when a key was *just* pressed.



