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
whenkeypressed...Jumpscript...
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
whenup arrowkeypressedYourjumpscriptherewhenleft arrowkeypressedchangexby

However if you want smooth left movement use
whenup arrowkeypressedYourjumpscriptherewhenleft arrowkeypressedrepeatuntilNotkeyleft arrowpressedchangexby
AcousticGuy
Scratcher
87 posts

How To Press Two Keys at the Same Time

Here is how I would do it:
whenclickedforeverifkey Spacepressed?thenYourjumpscript:ifkey Right Arrowpressed?thenmove5stepsifkey Spacepressed?andkey Right Arrowpressed?thenmove5stepsYourjumpscript:
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/
whenup arrowkeypressedifjumping?=0thensetjumping?to1sety velto15changeybyyvelchangey velby0repeatuntiltouchingcolor?changeybyyvelchangey velby-1gotogroundwhenclickedsetjumping?to0sety velto0setx velto0foreversetx veltoxvel*0ifabsofxvel<.2thensetx velto0changexbyxvelifkeyright arrowpressed?thenifyvel=0thenchangex velby1elsechangex velby0ifkeyleft arrowpressed?thenifyvel=0thenchangex velby-1elsechangex velby0iftouchingedge?thensetx veltoxvel*-1turnxvel*2degreesifonedge,bouncegotogrounddefinegotogroundiftouchingcolor?thenrepeatuntilnottouchingcolor?changeyby1changeyby-1sety velto0setjumping?to0
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!
foreverifkeyright arrowpressed?thenbroadcastright donot put the broadcast and waitifkeyup arrowpressed?thenbroadcastup
whenIreceiveright. . .whenIreceiveup. . .

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?

whenclickedforeverifkeyKEY1pressed?andkeyKEY2pressed?thenDoAction

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?

whenclickedforeverifkeyKEY1pressed?andkeyKEY2pressed?thenDoAction
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
whenkeypressed...Jumpscript...
That won't work smoothly. The forever script runs better.

foreverifkeyWpressed?thenJumpifkeyDpressed?thenRightifkeyApressed?thenLeft

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.
whenclicked Self explanatory sideways movement.foreverifLeftMovementKeythenMoveLeftifRightMovementKeythenMoveRightwhenclicked This script is for jumping only because jump scripts usually are long and would give a delay if mixed with other scripts. foreverifJumpKeyPressedthenJump Ground detections should be placed here too.
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