Intercepting many arrow keys (SOLVED)

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

Post Reply
MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Intercepting many arrow keys (SOLVED)

Post by MaxV » Fri Jul 18, 2014 3:59 pm

Hello,
I was trying to muove a car on the screen, but I can't understand how to intercept 2 pressed arrow keys.
The following code works only with one arrow key at time. If I'm rotating the car, I can't start moving:
##start card code###
on opencard
muovimac
end opencard

on arrowkey puls
switch puls
case "up"
put the velocita of me into v
add 10 to v
set the velocita of me to v
break
case "down"
put the velocita of me into v
add -10 to v
set the velocita of me to v
break
case "left"
put the angle of image "car" into temp
add 5 to temp
set the angle of image "car" to temp
break
case "right"
put the angle of image "car" into temp
add -5 to temp
set the angle of image "car" to temp
break
end switch
end arrowkey

on muovimac
#prendiamo la velocita'
put the velocita of me into v
put the angle of image "car" into temp
#creaimo una strina del tipo x,y
put -v * sin( pi / 180 * temp) into movimento
put "," after movimento
put -v * cos( pi / 180 * temp) after movimento
move image "car" relative movimento
if v > 0 then
add -1 to v
end if
if v < 0 then
add 1 to v
end if
set the velocita of me to v
send muovimac to me in 0.02 sec
end muovimac
###end card code######
Last edited by MaxV on Wed Jul 23, 2014 10:56 am, edited 1 time in total.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Intercepting many arrow keys

Post by Klaus » Fri Jul 18, 2014 6:44 pm

Buonasera Max,

you can only "catch" ONE arrowkey at a time, not two!
Maybe you can use an additional key (r = rotate?) and "catch" "keysdown()"?


Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: Intercepting many arrow keys

Post by dunbarx » Sat Jul 19, 2014 3:15 pm

Max.

Is your idea to be able to move the car on the diagonal, and that is why you want to be able to press, say, the "up" and "left" at the same time to move to the upperLeft?

If so, I would suggest that you use the mouse pointer inside a small rectangle, where the position of the pointer in that space moves the car. In this way you can completely control both the direction and speed of the car. This would be a very fun task. Do you see what I mean?

Craig Newman

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Intercepting many arrow keys

Post by [-hh] » Sun Jul 20, 2014 4:45 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 3:41 pm, edited 1 time in total.
shiftLock happens

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10102
Joined: Fri Feb 19, 2010 10:17 am

Re: Intercepting many arrow keys

Post by richmond62 » Sun Jul 20, 2014 4:48 pm

Here's a thought:

Use keyDown and trap for the 1,2,3,4,6,7,8 and 9 keys on the numeric keyPad.

This may prove problematic and you may have to use rawKeyDown.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10102
Joined: Fri Feb 19, 2010 10:17 am

Re: Intercepting many arrow keys

Post by richmond62 » Sun Jul 20, 2014 5:01 pm

This should help with rawKey codes:
KD.png
KEYDOWNER.rev.zip
Here's the stack.
(18.58 KiB) Downloaded 202 times

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Intercepting many arrow keys

Post by [-hh] » Sun Jul 20, 2014 6:30 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 3:40 pm, edited 1 time in total.
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: Intercepting many arrow keys

Post by dunbarx » Sun Jul 20, 2014 7:09 pm

All.

I still say a square "field" where the mouse pointer is moved around is the most fun. It also allows speed control, since if you move the pointer, say, at a 30° angle from a center point, you can direct the car in exactly that direction, but also the distance from that center could set the velocity.

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10102
Joined: Fri Feb 19, 2010 10:17 am

Re: Intercepting many arrow keys

Post by richmond62 » Sun Jul 20, 2014 9:06 pm

Here's my take on this:

a stack with 2 methods.

Crack open the card scripts and have fun.
bug.png
Attachments
multi_keys.livecode.zip
Here's the stack.
(21.19 KiB) Downloaded 229 times

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Intercepting many arrow keys

Post by MaxV » Mon Jul 21, 2014 8:33 am

The keysdown solution is the best solution, you can move a car (an image) with just this code:

on arrowkey puls
put the keysdown into temp2
repeat for each item tItem in temp2
switch tItem
case "65362"
put the angle of image "car" into temp
#creaimo una strina del tipo x,y
put -5 * sin( pi / 180 * temp) into movimento
put "," after movimento
put -5 * cos( pi / 180 * temp) after movimento
move image "car" relative movimento
break
case "65364"
#dobbiamo muoverla in indietro rispetto al verso del muso
#prendiamo l'angolo della direzione
put the angle of image "car" into temp
#creaimo una strina del tipo x,y
put 5 * sin( pi / 180 * temp) into movimento
put "," after movimento
put 5 * cos( pi / 180 * temp) after movimento
move image "car" relative movimento
break
case "65361"
put the angle of image "car" into temp
add 1 to temp
set the angle of image "car" to temp
break
case "65363"
put the angle of image "car" into temp
add -1 to temp
set the angle of image "car" to temp
break
end switch
end repeat
end arrowkey
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Intercepting many arrow keys

Post by [-hh] » Mon Jul 21, 2014 9:39 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 3:40 pm, edited 1 time in total.
shiftLock happens

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10102
Joined: Fri Feb 19, 2010 10:17 am

Re: Intercepting many arrow keys

Post by richmond62 » Tue Jul 22, 2014 6:38 am

"Richmond, I would like to add a "else pass rawkeydown" to your code. Without this and with a fast repeat rate the spooling of the keyboard buffer is not interruptable."

As I spent no more than 20 minutes knocking that stack together just to illustrate 2 ways of moving an object that is probably the least of
its problems :)

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: Intercepting many arrow keys

Post by [-hh] » Tue Jul 22, 2014 7:28 am

..........
Last edited by [-hh] on Wed Aug 13, 2014 3:40 pm, edited 1 time in total.
shiftLock happens

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Intercepting many arrow keys (SOLVED)

Post by MaxV » Wed Jul 23, 2014 11:01 am

An old trick about speed up animations is the following: cheat the user eyes!
If your machine is slow, increase angle rotation steps and translation steps 8)
For example, rotation code from:

Code: Select all

put the angle of image "car" into temp
add 1 to temp
set the angle of image "car" to temp
became:

Code: Select all

put the angle of image "car" into temp
add 5 to temp
set the angle of image "car" to temp
and so on... You can obtain incredible effects deceiving user sensations.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Post Reply