Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Length of key being pressed
- KlohaJo
-
10 posts
Length of key being pressed
Hi everybody,
is there a way to actually calculate HOW LONG a key is being pressed?
I want a figure in a j'n r game make jump and make the jump height correlate to the length the “arrow up” key is pressed.
Thanks for any hints!
Johannes
is there a way to actually calculate HOW LONG a key is being pressed?
I want a figure in a j'n r game make jump and make the jump height correlate to the length the “arrow up” key is pressed.
Thanks for any hints!
Johannes
- awesome-llama
-
1000+ posts
Length of key being pressed
There are lots of ways to do it.
The first thing I think of is using the built-in timer block. You could reset the timer and when the key is released, check what the timer has reached.
The maths to convert a time in seconds to a jump height will have to be decided but I can't provide any answer because I don't know what you want it to be. You'll probably also want to have a limit to how high a jump can be, an if-else block is all that's needed for it.
The first thing I think of is using the built-in timer block. You could reset the timer and when the key is released, check what the timer has reached.
... // start this script when the key is pressed
reset timer
wait until <not <key [up arrow v] pressed?>>
say (join (join [key held for ] (timer)) [ secs]) for [2] secs // replace this with your jump code
The maths to convert a time in seconds to a jump height will have to be decided but I can't provide any answer because I don't know what you want it to be. You'll probably also want to have a limit to how high a jump can be, an if-else block is all that's needed for it.
- ItsMarlowTime
-
16 posts
Length of key being pressed
If you have nothing else being used for the timer, Then awesome-llama’s code should work just fine
If you are using it already, then what you can do is have 3 variables, one named “Time”, another named “TickTime” and another named “HeldTime”
(For a better explanation of what tick is used for, Look into what tick-based projects are)
and in another block section
The rest of your project should be able to function like it normally would
Hope this helps
If you are using it already, then what you can do is have 3 variables, one named “Time”, another named “TickTime” and another named “HeldTime”
(For a better explanation of what tick is used for, Look into what tick-based projects are)
when Flag clicked
set [Time v] to (0)
other code in this area if necessary, this is only ran once!
forever
broadcast [tick v] and wait
change [Time v] by (Timer)
set [TickTime v] to (Timer)
reset timer
end
and in another block section
when I receive [tick v]
If <key [Up Arrow v] pressed?> then
change [HeldTime v] by (TickTime)
else
if <<not <key [any v] pressed?>> and <not <(HeldTime)=(0)>>> then
Say (join(join[Key Held for ](HeldTime))[ seconds!]
set [HeldTime v] to (0)
end
end
The rest of your project should be able to function like it normally would

Hope this helps

Last edited by ItsMarlowTime (March 8, 2023 18:26:36)
- ARandomNumberIs7482
-
1000+ posts
Length of key being pressed
Hi everybody,
is there a way to actually calculate HOW LONG a key is being pressed?
I want a figure in a j'n r game make jump and make the jump height correlate to the length the “arrow up” key is pressed.
Thanks for any hints!
Johannes
when green flag clicked
set [len of key press v] to [0]
forever
if <key [any v] pressed?> then
reset timer
wait until <not <key [any v] pressed?>>
set [len of key press v] to (timer)
end
end
Last edited by ARandomNumberIs7482 (March 9, 2023 02:22:31)
- KlohaJo
-
10 posts
Length of key being pressed
Thanks for all your hints! That is helpful!
- LoGaNO_o
-
74 posts
Length of key being pressed
Hi everybody,
is there a way to actually calculate HOW LONG a key is being pressed?
I want a figure in a j'n r game make jump and make the jump height correlate to the length the “arrow up” key is pressed.
Thanks for any hints!
Johannes
You could do
when [Put your key here...] key pressed
set [Time Pressed] to [0]
repeat until <not <key [Put the same key here...] pressed?>>
wait (0.01) secs
change [Time Pressed] by (0.01)
end
I also have an example project for you if you'd like
https://scratch.mit.edu/projects/817854342/
Last edited by LoGaNO_o (March 13, 2023 13:11:59)
- Discussion Forums
- » Help with Scripts
-
» Length of key being pressed