Maneuver within a grid of fields

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
wdeacon
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1
Joined: Tue Mar 20, 2012 7:55 pm

Maneuver within a grid of fields

Post by wdeacon » Sun Oct 12, 2014 5:00 pm

I have been playing with a crossword puzzle app. I have #rows x #columns sets of boxes in which to enter the letters. These are groups containing a grc (to color in blank spaces), an index (numbering the across or down beginnings) and a field for the Letter. Livecode will naturally allow the player to tab through the letter field of the boxes/grps using the TAB key, but I would like to also use the arrow keys. An "on arrowkey the key" type handler works, except I do not know how to change the focus from the former letter field to the new one found using arrow keys. I am confused about "focus" and how to enter data in a field without using the mouse or the tab key.

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

Re: Maneuver within a grid of fields

Post by dunbarx » Mon Oct 13, 2014 12:30 am

Hi.

Do you have your fields ordered in any grid-like way? If not, you need to. For example, you could name the fields "R1 C3" or "R4 C5" (row 1 column 3, etc,)

Then, since you have a fixed array of fields, you always know what the neighbors are named. If the cursor was in one of the examples above, say "R1C3", then you can write a card handler that traps the "arrowkey" message, along with its parameter, and select the text of the appropriate adjacent field. So if the parameter was "down" you would select the text of the field who's name had its numerical part of the first word in its name one more than the original.

Where is the trap in this method? How do you fix it? Would you have found it on your own? Sometimes it is difficult for the author to test his own work.

Look up "focus", "selectedText" and "select" in the dictionary. You can use any of these to change the cursor location in a field. I suggest you make it work with all three, just for practice. Write back if you get stuck, but only after you do get stuck.

Craig Newman

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Maneuver within a grid of fields

Post by Simon » Mon Oct 13, 2014 12:41 am

Hi Craig,
This one caught my attention as well but I just stared at it. Of course R1 C1 etc. where was me head this morning. :roll:
Look up "focus", "selectedText" and "select" in the dictionary
I'd add openField and closeField as the fields are unlocked.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

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

Re: Maneuver within a grid of fields

Post by [-hh] » Mon Oct 13, 2014 1:57 pm

Hi all.

You could try this.
As soon as you have the name of the field you would like to go, say field fName, you could do:

select text of fld fName (which text is one char in your case)

If you are editing the field, then arrowkeys work inside there only.
One way to avoid this is to use the field's script.

Code: Select all

on arrowkey k 
   myArrowkey k
end arrowkey
-- where myArrowkey is that handler in card script (or group script)
-- that determines fName and selects fld fName
For a more detailed messages handling I'd use the other handlers mentioned by Craig and Simon.
shiftLock happens

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

Re: Maneuver within a grid of fields

Post by jacque » Mon Oct 13, 2014 5:11 pm

Or use a behavior assigned to each editable field. This is a perfect example of a good use for behaviors.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply