Page 1 of 1

Bug reported relating to textchanged

Posted: Sat May 04, 2019 5:56 pm
by kaveh1000
I have reported a bug here:

https://quality.livecode.com/show_bug.cgi?id=22039

Summary:

Adding text to a field via a script does not invoke textchanged

Re: Bug reported relating to textchanged

Posted: Sat May 04, 2019 6:30 pm
by bogs
There are a number of events that do not invoke textChanged in a field. Cut/copy/paste, mouse clicks, etc. For most of those, though, rawKeyUp/Down *does* see the event.

Re: Bug reported relating to textchanged

Posted: Sat May 04, 2019 6:46 pm
by kaveh1000
But it seems to me this is a bug.

Cut also did not invoke textchanged but was fixed recently:

https://quality.livecode.com/show_bug.cgi?id=21802

Re: Bug reported relating to textchanged

Posted: Sat May 04, 2019 7:32 pm
by bogs
I'm sorry for not being clearer kaveh, I'm not saying it is not a bug, I'm just pointing out there are lots of events where it doesn't trigger, and that rawKey* does for most of them.

I was surprised to find it out as well, as I made the assumption that 'textChanged' *should* reflect text changing in the field under any circumstance, based on reading this description of it (Max's wiki)
Is dispatched by the field whenever a user (or simulated user) action causes the content of the field to change.

Handle the textChanged message if you want to perform an action when the content of a field changes.

The message is sent immediately after the input operation completes, but before a screen update is requested. The corresponding update occurs at the end of the first command in the textChanged handler. This means that you can lock the screen at the first line of the handler to delay the screen update (allowing you to modify the content of the field without any flicker).
<sic>
The textChanged message is sent after messages such as keyDown and pasteKey but before messages such as keyUp.

...so I agree it sounds like a 'bug', but until they fix it if you need to know when something is happening in the field, you can substitute rawKey* events.

Re: Bug reported relating to textchanged

Posted: Sat May 04, 2019 10:05 pm
by kaveh1000
Ah, got it. Sorry, my confusion!

OK, but how do I use rawkey to find out if a text has been changed using a handler? I can't use on rawkeydown in place of on textchanged can I?

Pls see my minimal file I submitted in the bug report. I have added a rawkeydown handler.

Kaveh

Re: Bug reported relating to textchanged

Posted: Sat May 04, 2019 10:37 pm
by bogs
First thing I'd do with your stack is change rawKey to call something else, not process what is happening and pop up a dialog

Code: Select all

on textchanged
   answer "Changed"
end textchanged

on rawkeyUp
  //  answer "Changed"
  yourHandlerHere
end rawkeyUp
For instance, you may have seen my basic text editing tutorial, part 1. In it, I used this very technique in place of textChanged. Here is the way I structured it -

Code: Select all

local lclContent

on openField
   put me into lclContent
   charCount; saveCheck; scrollCheck
end openField

on rawKeyUp
   charCount; saveCheck; scrollCheck --<-- calling 3 other handlers to deal with what changes...
end rawKeyUp

on tabKey
   put "    " before the selectedChunk
end tabKey

on saveNow
// rawKeyUp lets this handler update the count for every character compared to the fields original state... 
   if the number of characters of me > (the number of characters of lclContent + 400) then
      put "Save Now ?" into field "saved"
   else
      put "" into field "saved"
   end if
end saveNow

on charCount
// rawKeyUp lets this handler update for every stroke that produces a character...
   put the number of characters of me && "Characters" into field "charCounter"
end charCount

on scrollCheck
// rawKeyUp lets this handler update to see if we need a scrollbar...
   if the formattedHeight of me > the height of me then
      set the vScrollbar of me to true
   else
      set the vScrollbar of me to false
   end if
end scrollCheck
In that code, 'charCount', 'saveCheck', and 'scrollCheck' all not only have to know if the field is different, but they have to know no matter what is going on, which may not be just typing. Opening the field sends a check, but the only way I could tell if something was pasted using the mouse was in using rawKey to send the check after each raw key message was processed.

Using it, you can also determine if someone was using a key combo that you didn't want enabled, like ctrl + c, and using other handlers to deal with it, but there are other message handlers built in that do most of that. Raw key is just a more precise instrument.

Re: Bug reported relating to textchanged

Posted: Sun May 05, 2019 11:41 am
by kaveh1000
Thank you Bogs. I have watched the first tutorial and very useful. Is there a part 2?

Re: Bug reported relating to textchanged

Posted: Sun May 05, 2019 12:05 pm
by bogs
There is, but it is a bit more complicated to put up. I think I am going to introduce some smaller subjects because, even though the actual tutorial is easy, the intuition behind it is not for people coming from other languages (I know it wasn't easy for me to grasp, at any rate).

Re: Bug reported relating to textchanged

Posted: Sun May 05, 2019 3:41 pm
by jacque
The textChanged message is only sent after manual user input. Presumably if a script implements a change, it knows and will call the appropriate handlers as necessary. Cut was fixed because it's a user action.

Re: Bug reported relating to textchanged

Posted: Sun May 05, 2019 10:20 pm
by kaveh1000
Yes. In my case I can have a workaround by getting the external script doing the change to do what is necessary, as it knows text has changed. But I have to put this workaround for any script making the change. It would be nicer if the field could indicate a change no matter how that change was made.