-
1. Re: How to set caret (cursor) position in a textField from a KeyEvent...
Andrei1 May 15, 2011 7:39 AM (in response to 2m)Are you using the same textfield for both what user types and suggestions?
-
2. Re: How to set caret (cursor) position in a textField from a KeyEvent...
2m May 15, 2011 7:47 AM (in response to Andrei1)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....
-
3. Re: How to set caret (cursor) position in a textField from a KeyEvent...
Andrei1 May 15, 2011 8:23 AM (in response to 2m)I am not aware of any way to prevent this behavior but you can reverse it. Try and see of the following works for you. Code assumes that textfield instance name is tf:
var originalSelection:int = 0; tf.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHnadler, false, 100); tf.addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandler, false, 100); function onKeyDownHnadler(e:KeyboardEvent):void{ if (e.keyCode == Keyboard.UP || e.keyCode == Keyboard.DOWN) { originalSelection = tf.selectionBeginIndex | tf.selectionEndIndex; } } function onKeyUpHandler(e:KeyboardEvent):void{ if (e.keyCode == Keyboard.UP || e.keyCode == Keyboard.DOWN) { tf.setSelection(originalSelection, originalSelection); } } -
4. Re: How to set caret (cursor) position in a textField from a KeyEvent...
2m May 15, 2011 8:41 AM (in response to Andrei1)Tanks a lot. I'll give that a try if I cannot find a better solution. It is somehow similar to using my timer, but slightly more elegant.
M
-
5. Re: How to set caret (cursor) position in a textField from a KeyEvent...
Andrei1 May 15, 2011 8:50 AM (in response to 2m)You are welcome.
Timer is practically never an elegant solution in cases of interactivity workarounds IMO.
-
6. Re: How to set caret (cursor) position in a textField from a KeyEvent...
2m May 15, 2011 10:03 AM (in response to Andrei1)I totally agree. But (smart *** 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-behavi our-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
-
7. Re: How to set caret (cursor) position in a textField from a KeyEvent...
Andrei1 May 15, 2011 11:42 AM (in response to 2m)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 :-)
-
8. Re: How to set caret (cursor) position in a textField from a KeyEvent...
2m May 15, 2011 12:25 PM (in response to Andrei1)I really didn't mean to criticize you or the idea you offered. I still think it is a very good one. My last comment just meant, that I don't consider the case fully closed. Not so much because I "need" to find a perfect solution, but more because I hoped for even deeper insight - I just felt that ther might be a way to hinder the execution of "default" arrow-key behavior. It might come a situation, where this would be necesary.
You offfered a very good solution for my problem: Thank You! (I mean it!)
Still I would be interested in:
A: a way to set (new) focus to a textfield with the up-key without triggering the default cursor-movement in said textfield.
and/or
B. a way to supress default behavior of the arrow-keys (maybe tabs?) but still receiving the events to use them "manually"


