Select text on text field

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

simon.schvartzman
Posts: 135
Joined: Tue Jul 29, 2014 12:52 am

Select text on text field

Post by simon.schvartzman » Mon Jul 10, 2017 10:53 pm

Hi, I have a text field with the traversalOn set to true.

I would like the whole text to be selected when the field gets focus.

In other words I would like to replace all the existing text on the field when the user types new text.

I've tried

Code: Select all

on mouseUp
    select the text of me
close mouseUp
and (on an iPhone) I can see the text become selected for a short period of time and then the cursor goes to end of the text unselecting the rest of the text.

Any hints as how to do it?

Thanks
Simon

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4926
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Select text on text field

Post by dunbarx » Mon Jul 10, 2017 11:38 pm

Hi.

first off, there is no "close mouseUp". You must 'end mouseUp"

I cannot believe this compiled. Anyway, what happens if you change it?

Craig Newman

shaosean
Posts: 829
Joined: Thu Nov 04, 2010 7:53 am

Re: Select text on text field

Post by shaosean » Tue Jul 11, 2017 5:47 am

I think you want to handle that in the "openField" event

Code: Select all

on openField -- when clicking in the field, select all its text
  select text of the target
end openField

simon.schvartzman
Posts: 135
Joined: Tue Jul 29, 2014 12:52 am

Re: Select text on text field

Post by simon.schvartzman » Fri Jul 14, 2017 4:18 pm

@dunbarx
first off, there is no "close mouseUp". You must 'end mouseUp"

I cannot believe this compiled. Anyway, what happens if you change it?
You are right it doesn't compile as expected (I wrote it instead of copying the code and made the mistake). Anyway behavior doesn't change.

@shaosean

Code: Select all

on openField -- when clicking in the field, select all its text
  select text of the target
end openField
it behaves exactly the same way, text is not selected and cursor insertion point remains at the end of the text

Many thanks for both of you for answering, any other hints?
Simon

shaosean
Posts: 829
Joined: Thu Nov 04, 2010 7:53 am

Re: Select text on text field

Post by shaosean » Sat Jul 15, 2017 2:35 am

If that code doesn't work, then something is broken and needs to be looked at.. It's Livecode's example code..

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4926
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Select text on text field

Post by dunbarx » Sat Jul 15, 2017 2:30 pm

Hi.

I assume the field is locked and the handler is in its script? Because the handler will work.

Perhaps the issue is the way a locked field acts in comparison to an unlocked one? If the field is unlocked, invoking something like:

Code: Select all

select before text of fld yourField
Will show a blinking cursor at the loc of interest. But in a locked field, this does not appear.

Craig Newman

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3926
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Select text on text field

Post by jacque » Sat Jul 15, 2017 4:00 pm

First, does it work correctly on desktop and only fail on mobile? There are a number of selection issues with LC fields on mobile devices. In general it's best to use native input fields on mobile.

If the problem is also happening in the IDE then something else is wrong.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

simon.schvartzman
Posts: 135
Joined: Tue Jul 29, 2014 12:52 am

Re: Select text on text field

Post by simon.schvartzman » Sat Jul 15, 2017 9:34 pm

Team may be I'm missing something very basic and hope you could clarify.

With the attached script:

1 - The behavior is exactly the same on the IDE, Simulator and real device (iPhone)
2 - On the IDE if I "enter" the field via "TAB" it works as expected
3 - Entering the field via mouse (IDE and simulator) or finger (iPhone) doesn't select the text
4 - Having or not the field "locked" doesn't make any difference.
5 - I'm using LC 8.1.5 Indy

looking forward for your enlightened inputs...
Attachments
FieldSelect.zip
(1.68 KiB) Downloaded 18 times
Simon

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1988
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Select text on text field

Post by jmburnod » Sat Jul 15, 2017 10:15 pm

Hi All,
It seems that "send in time" is necessary in this case. It works for me (LC indy 8.1.3).

Code: Select all

on openField -- when clicking in the field, select all its text
   set the traversalOn of me to true
  -- doSelText the short name of the target-- doesn't work
   send "doSelText the short name of the target" to me in 2 milliseconds--works
end openField

on doSelText pFld
    select char 1 to -1 of fld pFld
end doSelText
The main question remains... do you can wait 2 milliseconds :D
Best regards
Jean-Marc

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3926
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Select text on text field

Post by jacque » Sat Jul 15, 2017 10:28 pm

I see. Whatever the engine does after "openField" is sent is causing the selection to disappear. I had to change the script of each field to this:

Code: Select all

on openfield
  send "doSelect" to me in 1 millisecond
end openfield

on doSelect
  select the text of me
end doSelect
That could be improved by passing the field name to a doSelect handler on the card or somewhere else in the message path.

Edit: Oops, I see I was too slow to respond. :) BTW, you don't need to set traversalOn, it's persistent.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

shaosean
Posts: 829
Joined: Thu Nov 04, 2010 7:53 am

Re: Select text on text field

Post by shaosean » Sun Jul 16, 2017 2:34 am

This should be reported as a bug..

PBH
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 79
Joined: Sun Feb 20, 2011 4:26 pm
Location: Vancouver Island, BC, Canada. ex.UK
Contact:

Re: Select text on text field

Post by PBH » Sun Jul 16, 2017 3:10 am

I'm not sure this would be classed as a bug, because it looks to me like the engine is controlling "normal" behaviour. i.e. Normally when you click in a field the insertion point is placed at the point where you click with the cursor, so this is cancelling the select text script immediately after the mouse is released. I went back to LC 5.5.5 and it shows the same behaviour.

If you click ON the text, you will see the text become selected momentarily and then it is de-selected as the mouse button is released, unless you move the cursor away from the text before releasing the mouse button, in which case the text stays selected.

No mouseUp message is received by the field, card or stack so I think the engine is using the mouseUp to set the insertion point to where it would normally be expected, I think this also explains why send in time works, but another option is to check for selectionChanged, because that's exactly what happens when the mouse is released, this works reliably for me;

Code: Select all

on openField
   selectAllText
end openField

on selectionChanged
   selectAllText
end selectionChanged

command selectAllText
   select the text of me
end selectAllText

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3926
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Select text on text field

Post by jacque » Sun Jul 16, 2017 4:21 am

No mouseUp message is received by the field, card or stack so I think the engine is using the mouseUp to set the insertion point to where it would normally be expected, I think this also explains why send in time works, but another option is to check for selectionChanged, because that's exactly what happens when the mouse is released, this works reliably for me
Mouse messages aren't sent in an unlocked field unless it's a right click. I agree though this doesn't sound exactly like a bug, even if it sort of looks like one.

I hadn't thought of using selectionChanged, good idea.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

shaosean
Posts: 829
Joined: Thu Nov 04, 2010 7:53 am

Re: Select text on text field

Post by shaosean » Sun Jul 16, 2017 7:04 am

Just because something has worked a particular way for a while, doesn't mean it's not a bug ;) This is either a bug because the insertion caret should be handled before the openField (for exactly this situation) or it's a documentation bug, as the sample code I posted earlier was directly from the API documentation, on the LiveCode.com website..

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1894
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

Re: Select text on text field

Post by richmond62 » Sun Jul 16, 2017 8:04 am

FS.png
Field selector.livecode.zip
Here's the stack
(20.99 KiB) Downloaded 20 times

Post Reply

Return to “Talking LiveCode”