Skip to main content
2m91_2
Inspiring
May 15, 2011
Question

How to set caret (cursor) position in a textField from a KeyEvent...

  • May 15, 2011
  • 1 reply
  • 6962 views

...that follows the up-Arrow.

Hi all,

I'm building part of an UI that suggests certain words based on to the user's input. (Quit like Google's suggestions) It works quite well but one detail bothers me:

When I have a list of suggestions I highlight the list-items by listening to the arrow up and down keys. When the fist element is highlighted, and the user presses the up arrow, I want the cursor to be in the Text(Input)Field again, but at the last position.

(I removed the focus from the InputField not to move the cursor in my singel line field to the first position when the up-arrow is pressed)

I set the cursor to the last Position using setSelection() but as it is the field receives the key-press after my event callback is executed and the pressing of the up-arrow in a single line field does what it always does, it places the cursor at the first position.

My somewhat dirty solution is to use a Timer to solve it, but a fast typer might still experience problems.

I considered using the CHANGE event, but as the arrow key does not actually change the content of the TextField, that is not working. Still I think that the solution might be along that way, as the CHANGE event is/would be at the end of the event-chain, where I need to do the repositioning of the cursor.

I did some research, but could not find something so I would be very glad I someone could point me into the right direction.

TA

M

This topic has been closed for replies.

1 reply

Inspiring
May 15, 2011

Are you using the same textfield for both what user types and suggestions?

2m91_2
2m91_2Author
Inspiring
May 15, 2011

No, I don't. I have one (Input)Textfield, that is the one I'm Talking about. the fields I use for suggestions aren't even selectable. Actually, I don't "touch" the in any way during the described process. During highlightnig of the Suggestinon I keep the focus "null".

Meanwhile I remembered some pages in Colins "Essential AS3" about preventing the default behavior, and reread it, but still i only found out how to change the default behavior of TEXT_INPUT, and the arrow keys obviously do not trigger that event....

Inspiring
May 15, 2011

I totally agree. But (smart ass that I tend to be - sorry) Your solution isn't perfect too, as for one thing, the Cursor most times blinks at least once at the "wrong" (first) position and I have to remove the KEY_UP listener (which is additional work.... ;-) ).

I found a thread over at http://stackoverflow.com/questions/1018259/how-do-you-prevent-arrow-up-down-default-behaviour-in-a-textfield, and I did more research, along that line. I tried Event priority and tinkered with the bubbling- and capturePhases using the stage and the textfield as targets. I even achieved the right order of events, but still I couldn't find a way to prevent the up-arrow from moving the cursor to the front within my event-chain..

(I even tried changing the textfield's type temporarily to dynamic with no luck at all)

Even if I can run with what we have, I still would be very interested in a "cleaner" solution.

Thanx

M


Well, "my solution" was not meant to be perfect to begin with for it is out of the context of aggregation with other interactions. Of course, result depends on such factors as architecture of your application and display list(s) structure in particular. With that said - I suspect, given that there is no way to natively remedy textfiled behavior in question, solution will be very much a custom fine-tuning based on trial and error (as well as level of perfection one pursues :-)