Page 1 of 1
Signal that text in field has changed
Posted: Mon Mar 21, 2011 7:44 am
by urbaud
Hi,
When text in a text field is changed, ie. new text is added, current text is changed or text is deleted, (even as small a change as one character) how would I have LiveCode AUTOMATICALLY tell me the text in the field has been changed. Would the code be in the field's script? I was fooling around with "SelectionChanged" but that doesn't really give me what I want. In essence, when the text in a field is changed, I want LC to automatically save the text in the field, as with something like: put field 3 into URL "file:test.txt". Any help would be appreciated.
Thanks,
Dan
Re: Signal that text in field has changed
Posted: Mon Mar 21, 2011 11:40 am
by Dixie
Hi Dan...
I have attached a stack to show a way to automatically save any changes you make in a field to a text file. the script to look at is in the fld.
Code: Select all
local theStartWords
on mouseEnter
put the text of me into theStartWords
end mouseEnter
on mouseLeave
put the text of me into theEndWords
/* if the text of the field has changed then write the text of the field to a text file */
if theStartWords <> theEndWords then
/* the defaultfolder has been set in the stack script */
put theEndWords into URL ("file:" & the defaultFolder & "/textFile")
end if
end mouseLeave
Hope that it helps with what you want to do...
be well
Dixie
PostScript:
BvG's answer is the way to go using the 'closeField' message... then all you would need to do is
Code: Select all
on closeField
put the text of me into URL ("file:" & the defaultFolder & "/textFile")
end closeField
Re: Signal that text in field has changed
Posted: Mon Mar 21, 2011 11:46 am
by BvG
Unfortunately, there is no such message.
You can of course look at the closeField message, but that only fires after you leave the field. So most likely you need to catch rawkeyDown, and then handle corner cases like drag/drop or copy/paste each individually.
Re: Signal that text in field has changed
Posted: Mon Mar 21, 2011 8:00 pm
by urbaud
Hi Dixie and BvG,
Dixie, thanks so much, you did it again! It's exactly what I needed. And, BvG, I'm not sure I understand your reply. Remember, you're talking to a relative newbie to Rev. Thanks so much to both of you for your help.
Dan
Re: Signal that text in field has changed
Posted: Tue Mar 22, 2011 12:37 am
by BvG
LiveCode is message based. That means, whenever something happens a message is sent. for example, when you open a stack the openStack message is sent. Often several messages are sent, so when you click the mouse some of the messages can be mouseUp, mouseStillDown, or mouseRelease, depending on circumstances.
When you "leave" a field by changing the focus (also known as selection) out of the field, there's two messages that can be sent. one is the exitField message, the other the closeField message. The first happens when the field is unchanged, the other happens when the field is changed.
However, there is no message that happens every time you change text. There is a rawKeyDown (and rawkeyUp) message, but that doesn't cover all cases, like dragging text, or copying text. Therefore you either have to add more code to a field then only rawKeyDown, or only do stuff when the selection is moved out of the field.
Re: Signal that text in field has changed
Posted: Wed Mar 23, 2011 2:19 am
by urbaud
Hi BvG,
I appreciate the time you took to explain the messages that are sent when a change occurs in a field. Do you think that the mouseEnter, mouseLeave and closeField code that Dixie suggests is sufficient given my original post? Also, one of the things that happened to me was I accidentally cleared the text field and "saved" it (I was interrupted or something), which over wrote the data in the field. I wasn't happy, needless to say. So I would also use something like the following to ensure that didn't happen again.
Code: Select all
if fld "t1" is empty then
put "No data in field - will not save"
else
put the htmlText of fld "t1" into url ("file:textFile")
end if
Again, thanks for the help.
Dan