Discuss Scratch

CursedWonton
Scratcher
5 posts

How To Press Two Keys at the Same Time

Hello, I'm new to Scratch. I have a problem I haven't been able to find help with. I was wondering how you could press two keys at the same time and have it work correctly. For example, when you want to jump up and go left at the same time, how do you do that without having to press the keys one at a time? It just seems like it would be more convenient to have the sprite doing two things at once without having to stall in between pressing buttons.
asivi
Scratcher
1000+ posts

How To Press Two Keys at the Same Time

Hi, if you have your movement's script (including jump) inside a forever loop, take out the jump and use
when [ v] key pressed
...Jump script...::grey
Camo-Dude
Scratcher
500+ posts

How To Press Two Keys at the Same Time

There are different ways in which you can do this but it depends on how you want it so i will give you script for both ways
if you want it where they can jump but must press the left keys multiple times to keep moving left use this
when [up arrow v] key pressed
Your jump script here

when [left arrow v] key pressed
change x by ()

However if you want smooth left movement use
when [up arrow v] key pressed
Your jump script here

when [left arrow v] key pressed
repeat until <Not<key [left arrow v] pressed>>
change x by ()
end
AcousticGuy
Scratcher
87 posts

How To Press Two Keys at the Same Time

Here is how I would do it:
when green flag clicked
forever
if <key [ Space] pressed?> then
Your jump script:
end
if <key [ Right Arrow] pressed?> then
move (5) steps
end
if <<key [ Space] pressed?> and <key [ Right Arrow] pressed?>> then
move (5) steps
Your jump script:
end
end
asivi
Scratcher
1000+ posts

How To Press Two Keys at the Same Time

Let me show you a working example https://scratch.mit.edu/projects/119956269/
when [up arrow v] key pressed
if <(jumping?) = [0]> then
set [jumping? v] to [1]
set [y vel v] to [15]
change y by (y vel)
change [y vel v] by (0)
repeat until <touching color [#0c4] ?>
change y by (y vel)
change [y vel v] by (-1)
gotoground :: custom
end
end

when green flag clicked
set [jumping? v] to [0]
set [y vel v] to [0]
set [x vel v] to [0]
forever
set [x vel v] to ((x vel) * (0))
if <([abs v] of (x vel)) < [.2]> then
set [x vel v] to [0]
end
change x by (x vel)
if <key [right arrow v] pressed?> then
if <(y vel) = [0]> then
change [x vel v] by (1)
else
change [x vel v] by (0)
end
end
if <key [left arrow v] pressed?> then
if <(y vel) = [0]> then
change [x vel v] by (-1)
else
change [x vel v] by (0)
end
end
if <touching [edge v] ?> then
set [x vel v] to ((x vel) * (-1))
end
turn cw ((x vel) * (2)) degrees
if on edge, bounce
gotoground :: custom
end

define gotoground
if <touching color [#0c4] ?> then
repeat until <not <touching color [#0c4] ?>>
change y by (1)
end
change y by (-1)
set [y vel v] to [0]
set [jumping? v] to [0]
end
CursedWonton
Scratcher
5 posts

How To Press Two Keys at the Same Time

Thank you all for your help, I appreciate it!
AwesomeCreator9875
Scratcher
100+ posts

How To Press Two Keys at the Same Time

You can use broadcasting too!
forever
if <key [right arrow v] pressed?> then
broadcast [right v] // donot put the broadcast and wait
end
if <key [up arrow v] pressed?> then
broadcast [up v]
end
end
when I receive [right v]
...
when I receive [up v]
...

Last edited by AwesomeCreator9875 (Sept. 13, 2016 15:35:15)

pillOfCode
Scratcher
1 post

How To Press Two Keys at the Same Time

i have trouble too with this and none of the examples are working
pengoo49
Scratcher
2 posts

How To Press Two Keys at the Same Time

How about this?

when green flag clicked
forever
if <<key [KEY1] pressed?> and <key [KEY2] pressed?>> then
Do Action
end
end

Last edited by pengoo49 (Oct. 6, 2017 21:32:30)

charlottelucy15
Scratcher
100+ posts

How To Press Two Keys at the Same Time

pengoo49 wrote:

How about this?

when green flag clicked
forever
if <<key [KEY1] pressed?> and <key [KEY2] pressed?>> then
Do Action
end
end
That could work actually. I can test the script to see if it works.
awesome5185
Scratcher
1000+ posts

How To Press Two Keys at the Same Time

asivi wrote:

Hi, if you have your movement's script (including jump) inside a forever loop, take out the jump and use
when [ v] key pressed
...Jump script...::grey
That won't work smoothly. The forever script runs better.

forever
if <key [W v] pressed?> then
Jump :: grey
end
if <key [D v] pressed?> then
Right :: grey
end
if <key [A v] pressed?> then
Left :: grey
end
end

This will make it so you can hold both keys at a time and have it go diagonal, although technically it's not, it's moving too fast for you to see anyway.
TG_Zero
Scratcher
2 posts

How To Press Two Keys at the Same Time

Last edited by TG_Zero (April 16, 2018 18:39:46)

ChefBoyardee117
New Scratcher
1 post

How To Press Two Keys at the Same Time

oy oy thanks for the help my dudes
TheCodePro123
Scratcher
100+ posts

How To Press Two Keys at the Same Time

Here's the best idea that can be done.
when green flag clicked // Self explanatory sideways movement.
forever
if <Left Movement Key> then
Move Left
end
if <Right Movement Key> then
Move Right
end

when green flag clicked // This script is for jumping only because jump scripts usually are long and would give a delay if mixed with other scripts.
forever // Ground detections should be placed here too.
if <Jump Key Pressed> then
Jump
end
end
lotheking
Scratcher
1 post

How To Press Two Keys at the Same Time

Thanks! but nothing works.
StrangeMagic32
Scratcher
1000+ posts

How To Press Two Keys at the Same Time

lotheking wrote:

Thanks! but nothing works.
please make your own topic.
you are more likely to get an answer that way.

Last edited by StrangeMagic32 (April 18, 2019 22:29:02)

tanish-vaidya
Scratcher
3 posts

How To Press Two Keys at the Same Time

THIS IS A PROBLEM I'M FACING
What if you have a condition for example, if black color touched then stop doing this action and do this action. With one key pressed it works fine, but when i tried adding two keys to move the tank diagonally, the tank moved fine, until it touched black, when it did so it would surpass them. To see my problem I'm putting the link to my game, if you find the right code please remix it and send the link to me by replying to this post. Thank you. ^_^
The link is - https://scratch.mit.edu/projects/274450571/


Last edited by tanish-vaidya (Aug. 12, 2019 03:52:42)

ResExsention
New Scratcher
1000+ posts

How To Press Two Keys at the Same Time

tanish-vaidya wrote:

THIS IS A PROBLEM I'M FACING
What if you have a condition for example, if black color touched then stop doing this action and do this action. With one key pressed it works fine, but when i tried adding two keys to move the tank diagonally, the tank moved fine, until it touched black, when it did so it would surpass them. To see my problem I'm putting the link to my game, if you find the right code please remix it and send the link to me by replying to this post. Thank you. ^_^
The link is - https://scratch.mit.edu/projects/274450571/



Please make your own topic. The last reply was from 2018, so your post is considered a “necropost”.

Sorry about that…
dkhr
Scratcher
41 posts

How To Press Two Keys at the Same Time

codeman1044
Scratcher
1000+ posts

How To Press Two Keys at the Same Time

Please don't spam or necropost.

Powered by DjangoBB