Buttons

Overview

A button is the most common control in a user interface. A person presses it, and something happens. In this tutorial, you will add buttons to a page, connect them to behaviors, let one behavior serve many buttons, change what a button says while the program runs, and make buttons that show a picture. Along the way, you will make sure every button works with a keyboard and a screen reader.

Goals

You have the following goals for this lesson:

By the end of this lesson, you can

  • Add buttons to a page and run code when they are pressed
  • Use one behavior for many buttons by checking which button was pressed
  • Change a button's name while a program runs, and make picture buttons
  • Give every button a clear name and description for screen reader users

Warm up

Find the buttons in an app or website you use. Read only their names, ignoring everything around them. Can you tell what each one does? Find one button whose name is clear on its own and one whose name only makes sense alongside the rest of the screen.

Vocabulary

You will be learning about the following vocabulary words:

Vocabulary
TermDefinition
ButtonA control that does something when a person presses it, by clicking, tapping, or pressing Space or Enter.
PressTo use a button: click it, tap it, or give it the focus and press Space or Enter.
NameThe text on a button. It is also what a screen reader says when the button has the focus.
DescriptionExtra text that a screen reader can read to explain what a control does.
FocusThe control that keyboard input goes to right now.
Picture buttonA button that shows an image instead of text. It still needs a name.

New Code to Learn

You will be using the following new pieces of code:

New Code to Learn
Quorum CodeCode ExampleExplanation
use Libraries.Interface.Controls.Buttonuse Libraries.Interface.Controls.ButtonLets your program use the Button class.
PAGE:AddButton(NAME)Button button = page:AddButton("Show Hello")Adds a button to a page and returns it.
PAGE:AddButton(NAME, FILE)page:AddButton("Add to favorites", "Images/star.png")Adds a button that shows a picture.
BUTTON:SetName(TEXT)button:SetName("Save")Changes the text on the button and what a screen reader says.
BUTTON:SetDescription(TEXT)button:SetDescription("Saves your work")Gives a screen reader more detail about the button.
BUTTON:SetBehavior(BEHAVIOR)button:SetBehavior(behavior)Runs this behavior each time the button is pressed.
cast(Button, ITEM)cast(Button, event:GetItem())Treats the item from a BehaviorEvent as a Button.

CSTA Standards

This lesson covers the following CSTA K–12 standards:

For teachers · 2026 CSTA K–12 Computer Science Standards

HS-PRO-TR-19

High School · Programming

Evaluate a computing technology's alignment with design specifications and responsible design values, including its correctness, effectiveness, and user experience.

Also in 7 other lessons

MS-PRO-TR-20

Middle School · Programming

Refine a computing technology based on user feedback to improve its usability and accessibility.

Also in 7 other lessons

Every standard, and the lessons that cover it

Explore

Buttons are everywhere: "Send", "Next", "Buy", "Play". Because people use them so often, they expect every button to work the same way. A button should look like it can be pressed. Its name should say what it does, usually with a verb, like "Save" or "Play Again". And it should work no matter how a person reaches it.

In Quorum, a person can press a button in any of these ways:

Ways to press a button
HowWhat to do
MouseClick the button
Touch screenTap the button
KeyboardPress Tab until the button has the focus, then press Space or Enter

When a button gets the focus, a screen reader says the button's name, and that it is a button. It may also read the button's description. That is why every button in this tutorial gets a clear name and a description when the name alone isn't enough.

A button on a page

You met this pattern in Forms and Behaviors. page:AddButton makes the button, places it on the page, and gives it back to you. SetBehavior(me) tells the button to run this class's Run action when it is pressed. Make a project with General, then App, and replace Main.quorum with:

use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Forms.FormBehavior
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Controls.Button
use Libraries.Game.Graphics.Label

class Main is FormBehavior
    action Main
        Form form
        Page page = form:GetMainPage()
        page:SetTitle("Greetings")

        Button button = page:AddButton("Show Hello")
        button:SetDescription("Shows a greeting below the button")
        button:SetBehavior(me)

        page:AddLabel("Greeting", "No greeting yet.")

        form:Display()
    end

    action Run(BehaviorEvent event)
        Page page = GetPage()
        Label greeting = page:GetLabel("Greeting")
        greeting:SetText("Hello world!")
    end
end

Run it. Press Tab until you reach the button, then press Space or Enter. The label below the button changes to "Hello world!". Press Tab once more to move to the label. A screen reader reads its new text.

Notice what you didn't have to do. The page chose the button's size and place, and it put the button in the Tab order. You never set a size, a position, or the focus yourself.

One behavior for many buttons

An app often has several buttons that do similar things. Instead of writing a behavior for each, you can write one and check which button was pressed. event:GetItem() gives you the item that was pressed. It comes back as an Item, so you cast it to a Button, then read its name.

use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Forms.FormBehavior
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Controls.Button
use Libraries.Game.Graphics.Label

class Main is FormBehavior
    action Main
        Form form
        Page page = form:GetMainPage()
        page:SetTitle("Pick a Color")

        Button red = page:AddButton("Red")
        red:SetDescription("Choose red")
        red:SetBehavior(me)

        Button green = page:AddButton("Green")
        green:SetDescription("Choose green")
        green:SetBehavior(me)

        Button blue = page:AddButton("Blue")
        blue:SetDescription("Choose blue")
        blue:SetBehavior(me)

        page:AddLabel("Choice", "You haven't picked a color yet.")

        form:Display()
    end

    action Run(BehaviorEvent event)
        Button pressed = cast(Button, event:GetItem())
        Page page = GetPage()
        Label choice = page:GetLabel("Choice")
        choice:SetText("You picked " + pressed:GetName() + ".")
    end
end

All three buttons run the same Run action. The text in the label depends on which button was pressed.

On a page, each button gets its own row. The Groupings tutorial, after Pages, shows how to put buttons side by side.

Changing what a button says

SetName changes the text on a button at any time, not just when you make it. Because the name is also what a screen reader says, the new name is read the next time the button gets the focus. This program counts how many times its button has been pressed. Add a file named CountBehavior.quorum:

use Libraries.Interface.Behaviors.Behavior
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Controls.Button

class CountBehavior is Behavior
    integer count = 0

    action Run(BehaviorEvent event)
        count = count + 1
        Button button = cast(Button, event:GetItem())
        button:SetName("Pressed " + count + " times")
    end
end

And use it in Main.quorum:

use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Controls.Button

Form form
Page page = form:GetMainPage()
page:SetTitle("Counter")

Button button = page:AddButton("Press me")
button:SetDescription("Counts how many times you press it")

CountBehavior counter
button:SetBehavior(counter)

form:Display()

The variable count belongs to the behavior object, so it keeps its value between presses.

Buttons with pictures

A button can show a picture. Put an image file, such as a PNG, in a folder named Images inside your project folder. Then give AddButton both a name and the image's path:

use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Forms.FormBehavior
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Controls.Button

class Main is FormBehavior
    action Main
        Form form
        Page page = form:GetMainPage()
        page:SetTitle("Favorites")

        Button star = page:AddButton("Add to favorites", "Images/star.png")
        star:SetDescription("Saves this song to your favorites")
        star:SetBehavior(me)

        form:Display()
    end

    action Run(BehaviorEvent event)
        output "Added to favorites"
    end
end

A picture button still needs a name. A screen reader can't describe the picture, so the name is the only way a screen reader user knows what the button does. Name it for what it does ("Add to favorites"), not for what it looks like ("Star").

Blocks

Making buttons everyone can use

Before you share a program with buttons, check each one:

  • Name: does it say what the button does, even with nothing else on the screen? Use a verb: "Save", not "OK" or "Button 1".
  • Description: if the name is short, does the description explain the result?
  • Keyboard: can you reach it with Tab and press it with Space or Enter?
  • Feedback: when the button is pressed, can a person tell something happened, without seeing the screen? Changing a label, which a screen reader user can reach with Tab, is one way.

Engage

In this activity, you and a partner build a small message board: an app with at least four buttons that each show a different word or phrase. The goal is to practice creating buttons, sharing one behavior between them, and giving each button a name and description that make sense. Use one behavior for all the buttons. When a button is pressed, show its message in a label on the page. Give every button a name and a description. Decide together what order the buttons should go in, and write down why.

When it works, swap with another pair. Test their message board with only the keyboard, then with a screen reader if you can. For each button, write down whether its name made sense on its own. Give the other pair your notes, and use the notes you get back to improve at least one of your own buttons.

Wrap up

The name of a button does two jobs: it is the text people see, and it is what a screen reader says. When might those two jobs pull in different directions? How would you choose a name that works for both?