Signal that text in field has changed

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
urbaud
Posts: 120
Joined: Tue Feb 24, 2009 12:10 am

Signal that text in field has changed

Post by urbaud » Mon Mar 21, 2011 7:44 am

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
urbaud

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Signal that text in field has changed

Post by Dixie » Mon Mar 21, 2011 11:40 am

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
Attachments
TextChange.zip
(1.82 KiB) Downloaded 245 times
Last edited by Dixie on Mon Mar 21, 2011 12:01 pm, edited 1 time in total.

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Re: Signal that text in field has changed

Post by BvG » Mon Mar 21, 2011 11:46 am

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.
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

urbaud
Posts: 120
Joined: Tue Feb 24, 2009 12:10 am

Re: Signal that text in field has changed

Post by urbaud » Mon Mar 21, 2011 8:00 pm

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
urbaud

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Re: Signal that text in field has changed

Post by BvG » Tue Mar 22, 2011 12:37 am

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.
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

urbaud
Posts: 120
Joined: Tue Feb 24, 2009 12:10 am

Re: Signal that text in field has changed

Post by urbaud » Wed Mar 23, 2011 2:19 am

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
urbaud

Post Reply