View Full Version : Find coordinates of text cursor . . .
arkum
November 23rd, 2005, 07:26 AM
Hello.
Is there any way of tracking the coordinates of a cursor in an input text box?
Thanks
eki
November 23rd, 2005, 10:16 AM
createTextField("myfield_txt", this.getNextHighestDepth(), 20, 20, 300, 20);
this.onEnterFrame = function(){
this.myfield_txt.text = "x: "+this._xmouse+" y: "+this._ymouse;
}
arkum
November 23rd, 2005, 11:51 AM
Thanks eki.
Should have explained myself better. I need to find out the coordinates of the curser that appears in an input text box when a user clicks in it. The blinking vertical line that moves along to the right of the last character typed. NOT the mouse pointer.
eki
November 23rd, 2005, 02:01 PM
Hi
Sorry, I misunderstood.
I don't know how to get the coordinates of the the blinking cursor (caret) but i guess you could hack it by using the 'Selection.getCaretIndex()' method to get know the number of characters that have been typed in and working out an approximation as you already know the x and y of the field.
cheers
arkum
November 23rd, 2005, 02:20 PM
I've tried that, and if all letters were the same width, it would work, but they are not, so it's impossible to get a constant ie: 1(Caret index) = 14 px.
This is really annoying, as the line spacing is fixed and therefore easy to work out.
Searched all over for a solution, but nothing so far . . .
eki
November 23rd, 2005, 02:44 PM
Yeah, I thought of that, if you need to be accurate that solution is not good.
Now that i think of it the problem is the horizontal coordinate so:
you can create a second textfield off-stage set to auto size with the same text and just get it's textWidth.
to get to know the number of lines you just have to keep a counter that increases every time the _width of the dummy textfield is that of the 'real' field. Don't forget to clear the text in the dummy textfield at that time and refill it with the caretIndex of the 'real' field.
Sorry, I don't know if I explained it very well...
Cheers
arkum
November 23rd, 2005, 04:15 PM
Hey eki.
Thanks for sticking with it. I don't quite understand what you mean. Finding the width/height of the text field is not the problem. If all the characters were the same width I could use something like:
textwidth/number of characters per line = pixel width per character
Then multiply it by how many characters into the line the caret is by using the caret index.
But different characters are different widths, so short of indexing the width of every single character in a font, this isn't going to work.
eki
November 24th, 2005, 06:11 AM
Hi Again,
I made a little example that shows what I mean. It only works out the x coord (it understands line breaks). You can work out the y easily. It may need some more work as I've done it really quickly but it illustrates the concept.
cheers
Prince
August 9th, 2007, 12:05 PM
Hi,
same problem here! The last solution supplied seems to be the only way around it and it is really the last resort and quite an unclean way of doing things.
Actionscript 3.0 has a new object entitled TextSnapShot with a method named hitTestTextNearPos to which you supply x and y coordinates and it return the letter closest to the coordinates. It is not the solution but it might be used in some way to sort out the problem.
There is also the getCharBoundaries method that return the x and y co-ordinates of a specific letter which best suites our requirements. However I cannot find a similair method for AS 2.0.
Any developments in the meantime arkum?
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.