Scripting a Button

A LiveCode application is driven by user actions, when the user interacts with your application, by clicking a button or typing in a field, LiveCode sends a message.

You decide what messages you want your program to respond to and add scripts which handle those messages.

This lesson shows you how to respond to some of the messages that are sent to buttons.

Create the Stack

Create the Stack

Create a new stack, name it 'Button Messages' and add a scrolling field and a button to the stack.

Name the field 'Text' and the button 'Red'.

Add Text to the Field

Add Text to the Field

Add some text to the field, in this example I used some Lorem Ipsum text but any text will be fine.

Open the Code Editor for the Button

Open the Code Editor for the Button

To open the Code Editor for the button:

1. Choose 'Edit Mode'

2. Select the button

3. Click the 'Code' button in the Toolbar

You can also open the Code Editor by right clicking the button and choosing 'Edit Script' from the menu.

Scripting the Button

Scripting the Button

There are a number of messages that are sent to buttons.

mouseEnter: sent when the mouse pointer enters an object

mouseMove: sent when the user moves the mouse within the object area

mouseDown: sent when the user presses the mouse button

mouseUp: sent when the user releases the mouse button

In this case we want something to happen on mouseUp, as an example we are going to change the text color of the field so we set the script to:

on mouseUp

set the textColor of field "text" to red

end mouseUp

Then click the 'Apply' button(1), the traffic light indicator should turn green, showing that the script has compiled.

Note: A yellow indicator means the script has unsaved changes, a red indicator means there is an error on the script.

Try the Button

Try the Button

Switch to 'Run' mode(1) and click the 'Red' button (2). The text color of the field changes to red.

Adding a mouseDown Handler

Adding a mouseDown Handler

You can handle more than one message in a button script, open the Code Editor of the button again and add a mouseDown handler that changes the text color of the field to blue.

on mouseDown

set the textColor of field "text" to blue

end mouseDown

Now when you switch to 'Run' mode and click the button the text turns blue when the button is pressed and red when the mouse is released.

Using the Message Path

Using the Message Path

In the lesson The LiveCode Message Path we mentioned that you can use the Message Path to group similar functionality and minimise repetitive coding.

What if we had 3 buttons that change the text color? We could add mouseUp handlers to each one but since they all do basically the same thing we can instead use the Message Path to handle the mouseUp message on the card instead.

Add 2 more buttons, 'Green' and 'Blue' to the stack and ensure that the scripts of all the buttons are empty. This is important because if a button has a mouseUp handler, even if it doesn't do anything, the message won't be passed up to the card.

Handling the mouseUp Message on the Card

Handling the mouseUp Message on the Card

Open the Script Editor for the card by choosing 'Card Script' from the Object menu. Alternatively you can right click on the card and select 'Edit Card Script' from the menu.

Add a mouseUp handler to the card script, within it we use the target function to check which button was pressed.

on mouseUp

local tColor

 

## Use the target function to check which button was pressed

## The short name of the buttons are red, blue and green

## so we can use that to set the color

put the short name of the target into tColor

set the textColor of field "text" to tColor

end mouseUp

Using a Custom Handler

Another option is to create a custom handler and call this from the buttons, passing the color as a parameter.

You use custom commands and custom functions the same way as any other command or function. You can execute a custom command simply by typing the name of the command you want to send.

When a custom command is called a message with the same name as the command is sent. You respond to a custom command's message by writing a message handler with the name of the command. If you don't specify an object, the message is sent to the object whose script is being executed, and then passes up the message hierarchy as normal.

Like a built-in command, a custom command is an instruction to LiveCode to do something. You can include parameters with a custom command by passing them after the name.

The colorText Custom Command

Open the Code Editor for the card and add the following custom command

command colorText pColor

set the textColor of field "text" to pColor

end colorText

This command takes a parameter, pColor, and sets the text color of the field to the value of that parameter.

Now open the script of button 'Red', change the mouseUp handler so it calls the custom command, passing the color as a parameter.

on mouseUp

colorText "red"

end mouseUp

Repeat this for the other buttons, changing the parameter in each case.

Calling the Custom Handler from the Card Script

An alternative option is to handle mouseUp on the card and call the custom command from the card script.

on mouseUp

put the short name of the target into tColor

colorText tColor

end mouseUp

13 Comments

Alex Thrower

Not changing red for me - compiles fine. Am I missing some earlier stage? Just trying the coding for buttons but can some good folks help - come form a VB background but seeking redemption.

avalmez

The example given above does not change the color of the text on my system (mac os x 10.7.2) running live code 4.5.3. it only changes the color of the carat.

Hanson Schmidt-Cornelius

Under step: "Create the Stack", you are required to provide the text field with the name 'Text'. You need to ensure that you have done this using the Property Inspector. The message may otherwise not be sent to the right location. You would also find that you get an error when you select the "Red" button.

Also try to ensure you use plain text, rather than pasting text that contains formatting information. You can do this by typing characters into the field, rather than copying and pasting text.

I tested this on Mac OSX 10.7.2 and LiveCode 4.6.4.

avalmez

the field is named text and i didn't notice any errors so i don't think it's a naming issue. i removed the pasted text and entered text directly into the field and it worked as before, i.e. text was not affected but color of carat was. i then deleted the existing field, added a new one named text, and typed directly into the field. this generated expected results. rather quirky don't you think? thanks for your response - it's nice to know the user community is active!

coskun karaca

Super..It is fantastic and very flexible..

Ian Stewart

hi

How do I switch between icons that a button might display using code?

Regards
Ian

Hanson Schmidt-Cornelius

Hi Ian,

there is a lesson that shows you how to animate graphics by cycling through a number of images on a button: http://lessons.runrev.com/s/lessons/m/4071/l/44418. I hope that is what you mean. This lesson possibly covers a bit more than you are looking for, but it does show you how to switch between images on a button using code.

Kind Regards,

Hanson

Rick

just like avalmez comment above, all my fields are named correctly and all button scripts have been removed. But to get this to work, I had to remove the text object and add it back again. Once I did this, all worked fine

Hanson Schmidt-Cornelius

Hi Rick,

In order for this to work you have to ensure that the text content is text without formatting. For example if you copied and pasted text from this lesson on OSX, then it is very likely that the text color would not change after hitting the "Red" button. This is because the text contains hidden formatting characters.

Try using "File -> Paste Unformatted" in LiveCode, if you paste text from another application.

Kind Regards,

Hanson

Filip

Hi, I am completely new to livecode and coding and I am following the tutorial from the beginning in order to learn as quickly and as much as possible. However, I cannot seem to get the the following code right:

==========================
on mouseUp
local tColor

## Use the target function to check which button was pressed
## The short name of the buttons are red, blue and green
## so we can use that to set the color
put the short name of the target into tColor
set the textColor of field "text" to tColor
end mouseUp
====================

Could somebody tell me how to complete this code? I have tried many different things (I assume I must use the if else, end if code in order to specify which button is pressed when?)

First: I do not understand what the local tColor stands for and why it is added into the function. Then I tried to use the target function to see which button is pressed (With target function one means 'if, end if', right? Otherwise I do not understand what is meant by target function)

I changed the 'shortname' in the above mentioned code into the values given. However nothing was happening. Only time something happened is that the text turned to green, no matter which button I pressed, but then it also stayed green, no matter which button I pressed. I am a little bit confused here. Did I mis something? I am following all the tutorials from the beginning in order to avoid these kind of 'obvious and simple' questions, but I feel as if I have missed something somewhere. I do not know for example what one means with 'the target function'

Thanks in advance,

Elanor Buchanan

Hi Filip

The target function tells you which control was clicked, the short name is a property of a control, in this case it will be either "red","blue" or "green". This is stored in the variable tColor which is then used to set the text color of the field.

I hope that helps.

Kind regards

Elanor

Joel Lim

The mouseUp Message Card script is working fine. However, I hit an error after I accidentally click outside the button anywhere on the card itself. tColor value was empty and thus an error occur.

Can somebody shows how to validate if tColor is empty do nothing?

Thanks.

Hanson Schmidt-Cornelius

Hi Joel,

you can test what control the mouse message came from and filter out any actions if it did not come from a button. For example try the following:

if the name of the target begins with button then
put the short name of the target into tColor
set the textColor of field "text" to tColor
end if

Kind Regards,

Hanson

Add your comment

E-Mail me when someone replies to this comment