How do I make a "if key press space or up or w key" kind of statement?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Starpat
Posts: 16
Joined: Sat Nov 16, 2019 12:09 pm

How do I make a "if key press space or up or w key" kind of statement?

Post by Starpat »

Code: Select all

on keyDown theKey
   if theKey is space then
      put -1 into birdDirection
   end if
end keyDown
Hello! :D I have a question. What if in the code I want to put a "if key space or key up or key w" how do I do that? Sorry If I'm a newbie but please help me please. I'm making a flappy bird game following a udemy course on livecode by the way.
8) Dude
bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: How do I make a "if key press space or up or w key" kind of statement?

Post by bogs »

Hello Starpat :D

Instead of typing out "space", try hitting the spacebar instead and wrapping it in quotes. For instance (tested)

Code: Select all

on keyDown theKey
   if theKey is "q" then move button 1 relative 0,+5
   if theKey is " " then move button 1 relative 0,-5
end keyDown
If you read the keyDown entry in the dictionary, the relevant part is -
Parameters:
The keyName is the actual character of the pressed key.
Image
richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10415
Joined: Fri Feb 19, 2010 10:17 am

Re: How do I make a "if key press space or up or w key" kind of statement?

Post by richmond62 »

Well here's the code in the cardScript that I used to trap for SPACE and 'w':

Code: Select all

on keyDown KD
   put empty into fld "fRESULT"
   if KD is " " then
      put "Space" into fld "fRESULT"
   else
      if KD is "w" then
         put "w" into fld "fRESULT"
      end if
   end if
end keyDown
I don't think you can trap in the same way for 'key up' as you suggest;
"if key space or key up or key w"
Attachments
multiKey.livecode.zip
Here's the stack
(965 Bytes) Downloaded 575 times
richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10415
Joined: Fri Feb 19, 2010 10:17 am

Re: How do I make a "if key press space or up or w key" kind of statement?

Post by richmond62 »

If by 'key up' you mean
the arrowKey up then that's altogether different:

Code: Select all

on arrowKey AK
if AK is "up" then
--do something
end if
end arrowKey
Starpat
Posts: 16
Joined: Sat Nov 16, 2019 12:09 pm

Re: How do I make a "if key press space or up or w key" kind of statement?

Post by Starpat »

Okay I'll try it.
8) Dude
richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10415
Joined: Fri Feb 19, 2010 10:17 am

Re: How do I make a "if key press space or up or w key" kind of statement?

Post by richmond62 »

Arrow keys are dealt with arrowKey,

other keys with keyDown

if you want to work with both arrowkeys and others in the same script then you'll
have to use rawKeyDown and find out their rawKey codes.
Attachments
KEY_CODES.livecode.zip
(1 KiB) Downloaded 546 times
Starpat
Posts: 16
Joined: Sat Nov 16, 2019 12:09 pm

Re: How do I make a "if key press space or up or w key" kind of statement?

Post by Starpat »

Code: Select all

local birdDirection
local birdVerticalSpeed
local gameIsRunning
local citySpeed
on preOpenCard
   put 1 into birdDirection
   put 4 into birdVerticalSpeed
   put 3 into citySpeed
end preOpenCard
on startGame
   preOpenCard
   put true into gameIsRunning
   flapBird
end startGame
on stopGame
   put false into gameIsRunning
   set the bottom of button "Bird" to 400
end stopGame
// move bird up and down
on flapBird
   // move bird
   put the bottom of button "Bird" into newPosition
   add(birdDirection * birdVerticalSpeed) to newPosition
   set the bottom of button "Bird" to newPosition
   // move City_Background1
   put the right of button "City_Background1" - citySpeed into newLoc
   if newLoc < 0 then
      set the left of button "City_Background1" to the right of button "City_Background2" - citySpeed
   else
      set the right of button "City_Background1" to newLoc
   end if
   // move City_Background2
   put the right of button "City_Background2" - citySpeed into newLoc
   if newLoc < 0 then
      set the left of button "City_Background2" to the right of button "City_Background1"
   else
      set the right of button "City_Background2" to newLoc
   end if
   
   //
   if gameIsRunning then
      send flapBird to me in 0.02 seconds
   end if
end flapBird
// control bird movements
on keyDown theKey
   if theKey is space then
      put -1 into birdDirection
   else
      if theKey is "w" then
         put -1 into birdDirection
      end if
   end if
end keyDown
on arrowKey AK
   if AK is "up" then
      put - 1 into birdDirection
   end if
end arrowKey
on keyUp
   put 1 into birdDirection
end keyUp
So I've tried and it kinda works but not quite still. My flappy bird in the game is meant to go up once with the keep press but when I use the up arrow key it continues to go up after I pressed it.
ssssssss212121.PNG
how do i put in image please too :| (edit: thanks for telling me :D )
Last edited by Starpat on Fri Nov 29, 2019 5:22 pm, edited 3 times in total.
8) Dude
richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10415
Joined: Fri Feb 19, 2010 10:17 am

Re: How do I make a "if key press space or up or w key" kind of statement?

Post by richmond62 »

I'll come back to you after supper.
richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10415
Joined: Fri Feb 19, 2010 10:17 am

Re: How do I make a "if key press space or up or w key" kind of statement?

Post by richmond62 »

Sorry, I got distracted last night.

Code: Select all

on rawKeyDown RAWK
   put empty into fld "fKEE"
   switch RAWK
      case "32"
         put "SPACE" into fld "fKEE"
         break
      case "119"
         put "w" into fld "fKEE"
         break
      case "65362"
         put "UP" into fld "fKEE"
         break
      default
         pass rawKeyDown
   end switch
end rawKeyDown
Attachments
keyTrapper.livecode.zip
Here's the stack
(996 Bytes) Downloaded 575 times
richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10415
Joined: Fri Feb 19, 2010 10:17 am

Re: How do I make a "if key press space or up or w key" kind of statement?

Post by richmond62 »

on startGame
seems odd as "startGame" is not standard LiveCode.

I cannot see where "startGame" is called inwith that script.
bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: How do I make a "if key press space or up or w key" kind of statement?

Post by bogs »

I would have to guess it is called from elsewhere, like a button script where the button is named 'Start' :D
Starpat wrote: Thu Nov 28, 2019 8:20 pm how do i put in image please too :|
I'm so attached...
I'm so attached...
Once you've done the above steps, place the cursor where you want the picture to show up in the reply and click the "Place inline" button :D
Image
Klaus
Posts: 14324
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How do I make a "if key press space or up or w key" kind of statement?

Post by Klaus »

bogs wrote: Fri Nov 29, 2019 11:50 am
Starpat wrote: Thu Nov 28, 2019 8:20 pm how do i put in image please too :|
ksnip_attachFiles.png
Once you've done the above steps, place the cursor where you want the picture to show up in the reply and click the "Place inline" button :D
Yes, but only after you have at least 10 postings!
So in your next (but one) posting you can add images, links etc.
bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: How do I make a "if key press space or up or w key" kind of statement?

Post by bogs »

Klaus wrote: Fri Nov 29, 2019 12:24 pm Yes, but only after you have at least 10 postings!
I knew I forgot something Image
Image
bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: How do I make a "if key press space or up or w key" kind of statement?

Post by bogs »

Map looks good, but tough to figure out how to play it without reading the code :wink:
Image
Post Reply