livecode.com
  • Home
  • Download
  • Developers
  • Academy
  • Community
  • Store
  • Support
  • About
LiveCode Lessons » Getting Started with LiveCode Development » Scripting a Button

Topics

  • Before you start 2
    • The Structure of a LiveCode Application
    • The LiveCode Message Path
  • Basic Concepts 7
    • Adding Objects to a Stack
    • Navigating Around a Stack
    • LiveCode Properties
    • Scripting a Button
    • Communicating with the User
    • Working With Text
    • Reading and Writing to File
  • LiveCode Objects 8
    • Using Buttons and Default Buttons
    • Using Fields
    • Using Radio Buttons
    • Using Option Menus
    • Text in the Tab Panel
    • Using Progress Bars
    • Using Images
    • Using Players
  • Building a User Interface 2
    • Using Button Icons and Background Images
    • Using Visual Effects
  • Building a Standalone Application 1
    • Building Standalone Applications
  • Your first LiveCode Application 1
    • Hello World! 3 ways
  • The next step - Creating a 3-5-7 Game 3
    • Game 3-5-7 - A Simple Interface
    • Game 3-5-7 - Basic Functionality
    • Game 3-5-7 - Game Logic

Last Updated

Oct 11, 2011

Download Lesson PDF

Other Resources

  • Getting Started with LiveCode

  • Get Up and Running with LiveCode
  • Getting Started with LiveCode Development
  • LiveCode Concepts

  • Features, concepts and aspects of LiveCode
  • LiveCode Lessons

  • How To - Step-By-Step Guides To Tasks In LiveCode
  • How To - LiveCode Server Tasks
  • How To - LiveCode Mobile Tasks
  • How To - LiveCode Sample Scripts
  • How to - LiveCode Marketplace Products
  • How to Purchase and License LiveCode
  • Tutorials

  • Creating a Video Library Application
  • Data Grid

  • LiveCode Data Grid
  • Data Grid Tips & Tricks
  • Converting the Stock Program

Comments

9 for this lesson

  • Prev: LiveCode Properties
  • Next: Communicating with the User

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 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 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

Zoom

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

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

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

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

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

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

  • Prev: LiveCode Properties
  • Next: Communicating with the User

Comments (9)

Alex Thrower Wednesday Dec 21 at 11:40 AM

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 Sunday Jan 08 at 10:09 PM

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 Monday Jan 09 at 02:34 AM

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 Monday Jan 09 at 06:46 PM

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 Wednesday Feb 01 at 05:46 AM

Super..It is fantastic and very flexible..

Ian Stewart Friday Sep 14 at 03:41 AM

hi

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

Regards
Ian

Hanson Schmidt-Cornelius Monday Sep 17 at 01:54 AM

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 Sunday May 05 at 09:09 AM

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 Friday May 24 at 09:42 AM

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

Add your comment

E-Mail me when someone replies to this comment
Brought to you by RunRev Ltd, Registered in Scotland, No. SC200728