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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10098
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 » Fri Nov 29, 2019 2:49 pm

That was a half-finished thing of mine designed to make my 9-12 year old victims
suffer: they were meant to read the code! 8)

The map is ripped-off from battle for Wesnoth. :D

https://www.wesnoth.org/
Last edited by richmond62 on Fri Nov 29, 2019 6:01 pm, edited 1 time in total.

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 » Fri Nov 29, 2019 5:25 pm

Klaus wrote:
Fri Nov 29, 2019 12:24 pm
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.
I was still able to add images with less than 10 postings so yeah.
8) Dude

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 » Fri Nov 29, 2019 5:32 pm

richmond62 wrote:
Fri Nov 29, 2019 2:49 pm
That was a half-finished thing of mine designed to make my 9-12 year old victims
suffer: there were meant to read the code! 8)

The map is ripped-off from battle for Wesnoth. :D

https://www.wesnoth.org/
so ur calling me 12. :lol:
Last edited by Starpat on Fri Nov 29, 2019 5:59 pm, edited 5 times in total.
8) Dude

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 » Fri Nov 29, 2019 5:36 pm

richmond62 wrote:
Fri Nov 29, 2019 8:11 am
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
but this doesn't match for the code in my game.
8) Dude

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10098
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 » Fri Nov 29, 2019 6:01 pm

No, it doesn't match the code in your game.

If I sorted your game out for you it would be my game. 8)

But with a bit of lateral thinking, you can use my code to help you with yours. :D

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10098
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 » Fri Nov 29, 2019 6:02 pm

so ur calling me 12.
At 57 years old that took me a week of work! :)

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 » Sat Nov 30, 2019 11:51 am

ok :wink:
8) Dude

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 » Sat Nov 30, 2019 12:47 pm

richmond62 wrote:
Fri Nov 29, 2019 6:01 pm
No, it doesn't match the code in your game.

If I sorted your game out for you it would be my game. 8)

But with a bit of lateral thinking, you can use my code to help you with yours. :D
I tried but it just didn't work here's a screenshot.
Capture.PNG
Another screenshot.
3232132132321321312321312.PNG
8) Dude

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10098
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 » Sat Nov 30, 2019 1:24 pm

Personally I always control movement of objects on-screen with the arrowKeys ONLY.

Space, w and the UP arrowKey seem an odd combination.

I do feel that, instead of posting pictures of code and so on, you would
be far better to describe the FUNCTIONALITY you want . . .

e.g: on pressing the UP arrowKey the 'bird' moves up the screen.

Certainly my experience with both clients and students/pupils over a 28 year period is that
the best way to understand things is to sit them down in front of a large table with a handful of toys
so they can demonstrate exactly what they want in concrete terms.

LiveCode is so very flexible, and is capable of achieving the same sort of thing in so many ways
that show me your code, or my showing you my code might be like you speaking in Serbian and my speaking
in Albanian: i.e. we'd end up in Kosovo at daggers drawn: not because either of us is 'wrong' but just because
your LiveCode and my LiveCode are different.

I do not think that enough work has been done on the methodology of this sort of communication.

Klaus
Posts: 14193
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 » Sat Nov 30, 2019 1:38 pm

Hi Starpat,

the message "rawkweydown" returns a NUMBER (the keycode of the pressed key) and not its "value"!
So take another look at Richmonds script and try again.
Hint: Really no need to QUOTE numbers!

Code: Select all

...
 switch RAWK
      case 32
        ## put "SPACE" into fld "fKEE"
        ## Do your thing here when user pressed -> SPACE
         break
      case 119
         ## put "w" into fld "fKEE"
         ## Samew here for -> w
         break
      case 65362
         ## put "Arrow key UP" into fld "fKEE"
        ## and here for -> ARROW KEY UP
         break
      default
         pass rawKeyDown
...
Best

Klaus

P.S.
Your "rawkeyUP" handler will OVERWRITE ALL VALUES for birdDirection you have just set in "rawkeydown"!

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 » Sat Nov 30, 2019 2:41 pm

Klaus wrote:
Sat Nov 30, 2019 1:38 pm
Hi Starpat,

the message "rawkweydown" returns a NUMBER (the keycode of the pressed key) and not its "value"!
So take another look at Richmonds script and try again.
Hint: Really no need to QUOTE numbers!

Code: Select all

...
 switch RAWK
      case 32
        ## put "SPACE" into fld "fKEE"
        ## Do your thing here when user pressed -> SPACE
         break
      case 119
         ## put "w" into fld "fKEE"
         ## Samew here for -> w
         break
      case 65362
         ## put "Arrow key UP" into fld "fKEE"
        ## and here for -> ARROW KEY UP
         break
      default
         pass rawKeyDown
...
Best

Klaus

P.S.
Your "rawkeyUP" handler will OVERWRITE ALL VALUES for birdDirection you have just set in "rawkeydown"!
I made my code like this and it finally worked! :D

Code: Select all

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 rawKeyUp
   put 1 into birdDirection
end rawKeyUp
8) Dude

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10098
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 » Sat Nov 30, 2019 6:11 pm

That is great to know! :D

I do think you would have found things easier using rawKeyDown
so you didn't have to invoke both keyDown and arrowKey
which might possibly interfere with each other.

Klaus
Posts: 14193
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 » Sat Nov 30, 2019 7:01 pm

Like this:

Code: Select all

on rawKeyDown tKey
 switch tKey
 
     ## SPACE, w and ARROW KEY UP do the same thing, so we can do this:
      case 32
      case 65362
      case 119
         put -1 into birdDirection
      break
    
      ## ANY other pressed keys:  
      default
          put 1 into birdDirection
      break
  end switch
end rawKeyDown
See, much cleaner and even shorter! :-)

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10098
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 » Sat Nov 30, 2019 7:15 pm

See, much cleaner and even shorter!
Not that I'd like to admit this in public, but Klaus has been
something of a mentor to me for the last 20 years re LiveCode. :D

Klaus
Posts: 14193
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 » Sat Nov 30, 2019 9:34 pm

richmond62 wrote:
Sat Nov 30, 2019 7:15 pm
Not that I'd like to admit this in public, but Klaus has been something of a mentor to me for the last 20 years re LiveCode. :D
Love you, honeybunny! 😘

Post Reply