Popup Menus

Overview

When you right-click a word in a word processor, or select it and press Shift+F10 or the Menu key on Windows, a small menu appears next to it, with commands like Cut, Copy and Paste. This is a popup menu, also called a context menu, because its commands fit the thing you are interacting with using the mouse or keyboard. Popup menus keep commands close to the content they change, without a menu bar. In this tutorial, you will attach a popup menu to a text field on a page, open one from a button, and add a submenu. You will also learn the keys that open a popup menu, and why a program should not hide a command only in a popup menu.

Goals

You have the following goals for this tutorial:

By the end of this lesson, you can

  • Create a PopupMenu and fill it with MenuItem commands
  • Attach a popup menu to a control with SetPopupMenu, so right-clicking or Shift+F10 opens it
  • Open a popup menu from your own code with OpenAt
  • Explain how a keyboard or screen reader user reaches the commands in a popup menu

Warm up

Right-click (or Ctrl+click on a Mac) on three different things on your computer: a file, a word in a document, and an empty part of the desktop. On Windows, you can also select each one and press Shift+F10 or the Menu key instead. Write down which commands appear each time. Why are they different? Which way is faster for you: the mouse or the keys?

Vocabulary

You will be learning about the following vocabulary words:

Vocabulary
TermDefinition
Popup menuA menu that appears on top of a program when it is asked for and closes when a command is chosen.
Context menuA popup menu whose commands fit the control or content it was opened from.
Right-clickPressing the right mouse button. It is the usual way to open a context menu with a mouse. Shift+F10 opens one from the keyboard on many controls.
Global positionThe x and y position of an item measured from the corner of the whole window.

New Code to Learn

You will be using the following new pieces of code:

New Code to Learn
Quorum CodeCode ExampleExplanation
PopupMenuPopupMenu menuCreates an empty popup menu. It is hidden until it is opened.
Add(MenuItem item)menu:Add(clear)Adds a command or a submenu to the popup menu.
SetPopupMenu(PopupMenu menu)field:SetPopupMenu(menu)Attaches a popup menu to a control. Right-clicking the control opens it, and so does Shift+F10 on controls such as text fields.
OpenAt(number x, number y)menu:OpenAt(100, 200)Opens the popup menu with its top-left corner at a position in the window.
OpenAtCursor()menu:OpenAtCursor()Opens the popup menu where the mouse pointer is.
Close()menu:Close()Closes the popup menu.
GetGlobalX(), GetGlobalY()number x = button:GetGlobalX()Returns an item's position measured from the corner of the window.

CSTA Standards

This tutorial covers the following CSTA 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-19

Middle School · Programming

Use systematic strategies to test, refine, and document changes to a computing technology to meet the intended purpose.

Also in 6 other lessons

S1-SWD-UX-04

Specialty I · Software Development

Refine a user interface design using accessibility and responsive design tools.

Also in 13 other lessons

Every standard, and the lessons that cover it

Explore

A popup menu is built from the same MenuItem objects as a menu bar, which you met in the Menus tutorial. The difference is where it lives:

  • a menu bar is always on screen, in a row of the page;
  • a popup menu is hidden until something opens it, appears on top of everything else, and closes as soon as a command is chosen, the user clicks somewhere else, or the user presses Esc (keys below).

You never add a popup menu to a page. You attach it to a control, and it shows itself when it opens.

A behavior that changes text

The commands in this tutorial change the text in a text field, which you met in the Text Fields tutorial. Add a new class named ChangeTextBehavior. It remembers which text field to change and what to do to it:

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

class ChangeTextBehavior is Behavior
    TextField field = undefined
    text mode = ""

    action Run(BehaviorEvent event)
        if field = undefined
            return now
        end

        text value = field:GetText()
        if mode = "upper"
            field:SetText(value:ToUpperCase())
        elseif mode = "lower"
            field:SetText(value:ToLowerCase())
        elseif mode = "clear"
            field:SetText("")
        end
        output "The text is now: " + field:GetText()
    end

    action SetTextField(TextField newField)
        field = newField
    end

    action SetMode(text newMode)
        mode = newMode
    end
end

Attaching a popup menu to a control

Now replace Main.quorum. It adds a text field to the page, makes a popup menu with three commands, then attaches the menu to the field with SetPopupMenu:

use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Controls.MenuItem
use Libraries.Interface.Controls.PopupMenu
use Libraries.Interface.Controls.TextField

class Main
    action Main
        Form form
        Page page = form:GetMainPage()

        page:AddLabel("Message label", "Message:")
        TextField field = page:AddTextField("Message")
        field:SetDescription("Type a message. Press Shift+F10 for text options.")
        field:SetText("Hello, popup menus!")

        PopupMenu menu
        menu:SetName("Text options")
        menu:Add(MakeCommand("Uppercase", "upper", field))
        menu:Add(MakeCommand("Lowercase", "lower", field))
        menu:Add(MakeCommand("Clear", "clear", field))

        field:SetPopupMenu(menu)

        form:Display()
    end

    action MakeCommand(text name, text mode, TextField field) returns MenuItem
        ChangeTextBehavior behavior
        behavior:SetTextField(field)
        behavior:SetMode(mode)

        MenuItem item
        item:SetName(name)
        item:SetBehavior(behavior)
        return item
    end
end

Run the program and right-click the text field, or press Tab until the field has the focus and press Shift+F10. With a click, the menu opens where you clicked; with Shift+F10, it opens at the text cursor. Choose Uppercase, and the text changes. When the menu closes, the focus goes back to the text field.

Notice that the text field's description tells the user that Shift+F10 opens the options. A screen reader reads the description, so a user who cannot see the screen learns that the menu exists.

Opening a popup menu with the keyboard

Many people open popup menus with the keyboard. People who don't use a mouse need the keys, and many others, such as programmers and other professionals who work mostly from the keyboard, use them because they are faster than reaching for the mouse. These keys open the popup menu attached to a control, when that control has the focus:

Keys that open a popup menu
ControlKeys that open its popup menu
Text fieldRight-click, or Shift+F10
Text boxRight-click, Shift+F10, or Ctrl+click on a Mac
TreeRight-click, Shift+F10, or Ctrl+click on a Mac
ButtonRight-click, or Ctrl+click on a Mac

With the mouse, the menu opens where you clicked. With Shift+F10, it opens at the text cursor.

Once a popup menu is open, it has the keyboard focus, but nothing in it is chosen yet. Press Down or Up first, then use these keys:

A popup menu's keys
KeyWhat it does
Down arrowChooses the first item or moves down one item.
Up arrowChooses the last item or moves up one item.
Right arrowOpens the submenu of the chosen item.
Left arrowCloses the submenu and goes back to its parent item.
Enter or SpaceRuns the chosen command or opens the chosen submenu.
EscCloses the submenu or closes the popup menu.

Check the Button row of the first table. A button has no key that opens its popup menu. If you attach a popup menu to a button, anyone using the keyboard cannot reach it. This is why a program should never put a command only in a popup menu. Next, you will fix this by opening the menu from your own code.

Opening a popup menu from code

Any behavior can open a popup menu with OpenAt(x, y). The numbers are a global position, measured from the corner of the window, and the menu opens with its top-left corner there. A button's GetGlobalX and GetGlobalY give the global position of its bottom-left corner, so the menu opens just below the button. Make a class named OpenMenuBehavior:

use Libraries.Interface.Behaviors.Behavior
use Libraries.Interface.Controls.Control
use Libraries.Interface.Controls.PopupMenu
use Libraries.Interface.Events.BehaviorEvent

class OpenMenuBehavior is Behavior
    PopupMenu menu = undefined
    Control anchor = undefined

    action Run(BehaviorEvent event)
        if menu not= undefined and anchor not= undefined
            menu:OpenAt(anchor:GetGlobalX(), anchor:GetGlobalY())
        end
    end

    action SetMenu(PopupMenu newMenu)
        menu = newMenu
    end

    action SetAnchor(Control newAnchor)
        anchor = newAnchor
    end
end

Then add a "Text options" button to the page. Add use Libraries.Interface.Controls.Button to the top of Main, and put these lines just after field:SetPopupMenu(menu):

        Button options = page:AddButton("Text options")
        options:SetDescription("Opens a menu of commands for the message")
        options:SetPopupMenu(menu)

        OpenMenuBehavior openBehavior
        openBehavior:SetMenu(menu)
        openBehavior:SetAnchor(options)
        options:SetBehavior(openBehavior)

Now the same menu opens three ways: right-clicking the field or the button, pressing Shift+F10 in the field, or pressing the button with a click, Space or Enter. Every user can reach every command. The page puts the button in the Tab order after the field, so a keyboard user finds it by pressing Tab.

Blocks

A popup menu can hold submenus too, built just like the submenus in a menu bar. Add the submenu to the popup menu first, then add its commands. This version of the menu lines in Main puts the two case commands in a "Change case" submenu:

        PopupMenu menu
        menu:SetName("Text options")

        MenuItem caseMenu
        caseMenu:SetName("Change case")
        menu:Add(caseMenu)
        caseMenu:Add(MakeCommand("Uppercase", "upper", field))
        caseMenu:Add(MakeCommand("Lowercase", "lower", field))

        menu:Add(MakeCommand("Clear", "clear", field))

Keep popup menus short. A long menu is slow to read with a screen reader and hard to use on a small screen. Usually, three to seven commands is plenty.

Engage

In this activity, you add a popup menu to a program you've already written, and check on your own that it works with the mouse and with the keyboard. The goal is to choose commands that fit a control's context, and to give everyone a clear way to reach them. Pick a control from one of your earlier programs, such as a list or a text box, and write down three commands that would make sense in its context menu. Then add a popup menu with those commands. Make sure every command can also be reached without a mouse, with Shift+F10 or a button that calls OpenAt.

When it works, test it both ways, with the mouse and with the keyboard, using this self-check. Write yes or no for each line, and what you did to find out:

  • I can reach the control with Tab, and open its menu with Shift+F10.
  • I can choose every command with the arrow keys and Enter.
  • After a command runs, the focus is back on the control.
  • The control's description tells a person that it has a menu, and how to open it.
  • Every command in the menu can also be reached some other way, such as a button or a menu bar.

For each "no", change your program and write down what you changed and why. Then run the whole self-check again.

Wrap up

Popup menus are fast for people who know they are there, with the mouse or the keyboard, and nearly invisible to everyone else. How does a user learn that a popup menu exists? Name two ways your program can tell them.

The next tutorial covers Toolbars, a row of buttons for the commands people use most.