Why arrowKey is not very useful

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
richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10099
Joined: Fri Feb 19, 2010 10:17 am

Why arrowKey is not very useful

Post by richmond62 » Tue Jul 14, 2020 8:51 pm

I have a set of online pupils/students just now who are trying to make simple games where the 'hero'
is controlled by the arrow keys on the computer keyboard a bit like this;

Code: Select all

on arrowKey AK
   put item 1 of the loc of grc "ANT" into LR
   put item 2 of the loc of grc "ANT" into UD
   switch AK
      case "up"
          set the backgroundpattern of grc "ANT" to the ID of img "A1.png"
         move grc "ANT" to LR,(UD-12)
         break
---and so on and so forth
  end switch
end arrowKey
BUT . . . the main problem is that if an end-user keeps pressing an arrow key the key keeps sending keyDown signals
which is not desirable.

This has been an ongoing problem for some years.

If we change our control keys to, let us say the W, A, S & Z keys we can overcome that problem by using keyUp rather than keyDown.

But arrowKey is a sort of kinky keyDown confined to the arrow keys, there is not the possibility of either

(pseudoCode) arrowKeyDown or arrowKeyUp

I have been chewing over this one for a long, long time (well, about 5 years on and off) and, reluctantly reached
the conclusion that using arrowKeywhen one wants to use the arrow keys is not very useful at all.

And doing this sort of thing when I want to use the arrow keys:

Code: Select all

on rawKeyUp XXX
put item 1 of the loc of grc "ANT" into LR
   put item 2 of the loc of grc "ANT" into UD
   switch XXX
      case "65362" --- this is the rawKey code for the 'up' arrow key
set the backgroundpattern of grc "ANT" to the ID of img "A1.png"
         move grc "ANT" to LR,(UD-12)
         break
---and so on and so forth
  end switch
end rawKeyUp

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

Re: Why arrowKey is not very useful

Post by richmond62 » Tue Jul 14, 2020 9:11 pm

It's time for a COMPARE & CONTRAST session:
-
Screenshot 2020-07-14 at 23.08.42.png
Attachments
ANT rawKeyUp.livecode.zip
Stack using rawKeyUp
(7.02 KiB) Downloaded 198 times
ANT arrowKey UP.livecode.zip
Stack using arrowKey
(7.04 KiB) Downloaded 216 times

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Why arrowKey is not very useful

Post by bogs » Tue Jul 14, 2020 9:55 pm

richmond62 wrote:
Tue Jul 14, 2020 8:51 pm
BUT . . . the main problem is that if an end-user keeps pressing an arrow key the key keeps sending keyDown signals
which is not desirable
I forget who it was, but someone (probably Craig, might even have been you!) had a solution for that.

I forgot now what it was, though, might have been to silently drop further key repeats that had piled up or some such. There were a variety of other ways to handle it listed in this topic... including a very bad example by yours truly.
Image

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

Re: Why arrowKey is not very useful

Post by dunbarx » Wed Jul 15, 2020 2:37 am

Richmond.

I don't understand. Trapping an arrowKey message will not also invoke a "keyDown" message.Add this to your script;

Code: Select all

on keydown tkey
   put tkey
end keydown
When I press the arrowKey, the image goes up. When I press, say "k", I get a "k" in msg. Where are you getting yours?

Craig

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

Re: Why arrowKey is not very useful

Post by richmond62 » Wed Jul 15, 2020 2:40 pm

When I press the arrowKey, the image goes up. When I press, say "k", I get a "k" in msg. Where are you getting yours?
The problem is not the arrow keys Ding an Sich, it is the generic problem that when a user keeps a key depressed
it sends repeated signals.

If one wishes to avoid one's stack getting more than a single key signal one has to use keyUp/rawKeyUp as in:

Code: Select all

on keyUp KUP
 -- do something
 end keyUp
The inherent problem about arrowKey is that it is really a cloaked keyDown, so to guarantee that
a user cannot send repeated signals (thus 'avoiding' later code) one must use rawKeyUp with the
raw key values for the arrow keys rather than using arrowKey at all.

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

Re: Why arrowKey is not very useful

Post by dunbarx » Wed Jul 15, 2020 5:02 pm

Ah.

So you meant "when the user keeps holding the arrowKey down" not "keeps pressing an arrow key".

OK.

The question is simply whether it is nobler in the mind to make kludges. This in the card script, on a card with a single button:

Code: Select all

on rawKeyup tkey
   if tkey = 65362 then set the stopMoving of this cd to ""
end rawKeyup

on rawKeyDown tkey
   if tkey = 65362 and  the stopMoving of this cd ="" then set the top of btn 1 to the top of btn 1 -5
   set the stopMoving of this cd to "true"
end rawKeyDown
Add the other values for the other arrowKeys.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7391
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Why arrowKey is not very useful

Post by jacque » Wed Jul 15, 2020 5:51 pm

I'd probably use a script local instead of a custom property, but either way Craig's method works. I had some bad experiences with custom props accidentally stored in the wrong state on save, so I avoid them for transient storage.

Another method would be to track the milliseconds and only respond if the interval is long enough. That would function more like a throttle to slow down the ant's movement.

The arrowKey message isn't really a cloaked keyDown, it's just that you get all the keypress messages of which arrowKey is just one.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Why arrowKey is not very useful

Post by dunbarx » Wed Jul 15, 2020 6:32 pm

Jacque.

I like that better as well. :wink:

Richard:

Code: Select all

local stopMoving

on rawKeyup tkey
   if tkey = 65362 then put "" into stopMoving
end rawKeyup

on rawKeyDown tkey
   if tkey = 65362 and  stopMoving ="" then set the top of btn 1 to the top of btn 1 -5
   put "true" into stopMoving
end rawKeyDown
Craig

Post Reply