Detecting changes in a field
It might on occasion be useful to detect when the content of a field changes. In this lesson we will see how to detect changes in a field by intercepting and handling messages.
How can you change the text in a field?
First we need to think about the ways that the text in a field can be changed
- dragging text into the field
- dragging text out of the field
So we need to handle the messages that are sent when any of these actions occur.
For this example we will create a simple stack with 2 fields on it. When the text in the "Source" field changes the current text of the "Source" field will be copied to the "Destination" field.
Create a new stack and add 2 scrolling fields. Using the Property Inspector name the fields "Source" and "Destination".
The rawKeyUp message
We will use the rawKeyUp message to intercept and handle key presses. In this case we are using rawKeyUp rather than keyUp as it is sent when backspace, delete, return, command-v etc are pressed.
The keyUp message is only sent when a character key is pressed, if we chose to use that we would need to handle return, tab, backspace, delete, enter and command and function keys separately.
When we receive a rawKeyUp message we want to copy the text of field "Source" to field "Destination". The key press has already been registered and the key entered in the field so we can simply copy the text and then pass the rawKeyUp message up the message path. Select the Source field, open the code editor for the field, and enter this script:
on rawkeyUp copySourceToDestination pass rawkeyUp end rawkeyUp
Put this copySourceToDestination handler on the card script:
on copySourceToDestination put field "Source" into field "Destination" end copySourceToDestination
Now switch to run mode and try typing in the "Source" field.
Dragging and dropping text
Text can also be dragged in and out of the "Source" field. LiveCode automatically handles the mechanics of dragging and dropping text between and within unlocked fields so all we need to do is detect when text is dropped into or dragged out of the "Source" field and update the "Destination" field.
Dropping text into the source field
When text is dropped into the "Source" field a dragDrop message is received. In this case we can't simply call copySourceToDestination though as the text has not yet been added to the field when the message is received. Instead we must allow the dragDrop message to continue up the message path to be handled by the engine and then copy the text.
We do this by sending the message copySourceToDestination to the card in 0 milliseconds. This allows the current handler to complete before the message is sent, in this case it means the dragDrop message is handled and the text entered into the field before we copy the text to the "Destination" field.
on dragDrop send "copySourceToDestination" to this card in 0 milliseconds pass dragDrop end dragDrop
Dragging text from the source field
This is very similar to dropping text into the field, in this case we handle the dragEnd message which is sent to the object a drag and drop started from, when the data is dropped. Again we let the message be handled before we copy the text to the "Destination" field.
on dragEnd send "copySourceToDestination" to this card in 0 milliseconds pass dragEnd end dragEnd