Getting the position of the cursor in a text field.

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
andyh1234
Posts: 476
Joined: Mon Aug 13, 2007 4:44 pm
Contact:

Getting the position of the cursor in a text field.

Post by andyh1234 » Thu Oct 15, 2009 3:45 pm

Sorry to ask, but im hitting a little brick wall with this one!

Is it possible to find the location of the cursor in a text box?

I just need it so I can return a user to the same cursor position after Ive run some other code that is causing the text box to lose its focus. When I focus back on the control it places the cursor at the end and not where it was, which is a little annoying for the user.

Id guess I need to use....

select after char tMyChar of field "myField"

to place the user where I want them, but how do I find out the value of tMyChar.

If its not possible, Ill try to figure out how to keep the focus on the control while executing the code, but the quick option for me at the moment would to simply log the position in a temporary variable, then pick it back up when the code is finished.

Thanks

Andy

andyh1234
Posts: 476
Joined: Mon Aug 13, 2007 4:44 pm
Contact:

Post by andyh1234 » Thu Oct 15, 2009 4:07 pm

Sorry, managed to get there in the end.

Just posting the solution I found in case its any use to anyone else...

Code: Select all

set the itemdel to space
put item 4 of the selectedchunk of field "myField" into tMyCharLoc

-- Code here that loses the focus...,  then to re-gain it...

select after char tMyCharLoc of field "myField"
Im sure there are more elegant ways to do it, but this seems to work!

Thanks

Andy

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Thu Oct 15, 2009 8:00 pm

You don't have to change the itemDelimiter - just use the word chunk

Code: Select all

put word 2 of the selectedChunk of field "Foobar" into tSelectionStart
put word 4 of the selectedChunk of field "Foobar" into tSelectionEnd
if tSelectionEnd < tSelectionStart then
  -- the cursor is currently between two chars
else
  -- there is actually a chunk of text selected
end if
HTH,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

andyh1234
Posts: 476
Joined: Mon Aug 13, 2007 4:44 pm
Contact:

Post by andyh1234 » Thu Oct 15, 2009 10:10 pm

Thanks Jan

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10094
Joined: Fri Feb 19, 2010 10:17 am

Re: Getting the position of the cursor in a text field.

Post by richmond62 » Sat Jan 29, 2022 5:47 pm

Insert.png
Attachments
INSERT.livecode.zip
Here's the stack.
(1.36 KiB) Downloaded 152 times

Post Reply