Page 1 of 1
Maneuver within a grid of fields
Posted: Sun Oct 12, 2014 5:00 pm
by wdeacon
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.
Re: Maneuver within a grid of fields
Posted: Mon Oct 13, 2014 12:30 am
by dunbarx
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
Re: Maneuver within a grid of fields
Posted: Mon Oct 13, 2014 12:41 am
by Simon
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.
Look up "focus", "selectedText" and "select" in the dictionary
I'd add openField and closeField as the fields are unlocked.
Simon
Re: Maneuver within a grid of fields
Posted: Mon Oct 13, 2014 1:57 pm
by [-hh]
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.
Re: Maneuver within a grid of fields
Posted: Mon Oct 13, 2014 5:11 pm
by jacque
Or use a behavior assigned to each editable field. This is a perfect example of a good use for behaviors.