Menus
Overview
Most desktop programs have a menu bar: a row of words such as File, Edit and Help along the top of the window. Choosing one opens a menu, a list of commands. Some commands open another, smaller menu called a submenu. Menus let a program offer many commands without filling the screen with buttons. In this tutorial, you will put a menu bar at the top of a page, fill it with menus and submenus, connect each command to a behavior, and add keyboard shortcuts. You will also learn how a keyboard or screen reader user moves through a menu.
Goals
You have the following goals for this tutorial:
By the end of this lesson, you can
- Build a
MenuBarout ofMenuItemobjects, with menus and submenus, and put it on a page - Connect a menu command to a behavior so that choosing it does something
- Add mnemonics and keyboard shortcuts to menu items
- Use
GetMenuPathto find out where a chosen command sits in the menu - Compare the roles that people, companies and a government played in shaping menus and windows
Warm up
Open a program you use often, like a word processor or a web browser, and explore its menu bar. Which menus does it have? Pick one command and write down every step it takes to reach it. Is there a faster way, like a shortcut shown next to the command? Why do you think most programs put File first and Help last?
Vocabulary
You will be learning about the following vocabulary words:
| Term | Definition |
|---|---|
| Menu bar | A row of menu names along the top of a window. |
| Menu | A list of commands that opens when you choose a name on the menu bar. |
| Submenu | A menu inside another menu. It opens to the side of the item that holds it. |
| Menu item | One entry in a menu bar or menu. It is either a command or the name of a submenu. |
| Mnemonic | A single letter that chooses a menu item when the menu has the keyboard focus. |
| Shortcut | A key combination such as Ctrl+D that runs a menu command without opening the menu. |
New Code to Learn
You will be using the following new pieces of code:
| Quorum Code | Code Example | Explanation |
|---|---|---|
| MenuBar | MenuBar bar | Creates an empty menu bar. It stretches across the full width of whatever holds it. |
| Add(Item2D item) | page:Add(bar) | Adds any control to a page, in its own row. A page has no AddMenuBar action, so this is how a menu bar goes on a page. |
| MenuItem | MenuItem animals | Creates a menu item. It becomes a menu as soon as you add other items to it. |
| Add(MenuItem item) | bar:Add(animals) | Adds a menu item to a menu bar or to another menu item. |
| SetBehavior(Behavior behavior) | item:SetBehavior(behavior) | Sets the behavior that runs when the menu item is chosen. |
| SetMnemonic(text mnemonic) | animals:SetMnemonic("a") | Sets the letter that chooses this item from the keyboard. |
| SetShortcut(InputSet set) | dog:SetShortcut(dogKeys) | Makes a key combination run this item and shows it next to the item's name. |
| GetMenuPath() | Array<MenuItem> path = item:GetMenuPath() | Returns every item from the menu bar down to this one, in order. |
CSTA Standards
This tutorial covers the following CSTA standards:
For teachers · 2026 CSTA K–12 Computer Science Standards
High School · Programming
Create a modular program that uses procedures, external libraries, or objects to improve reusability and readability.
Also in 9 other lessons
Middle School · Programming
Use a procedure to structure code for clarity and reusability.
Also in 5 other lessons
Middle School · Programming
Refine a computing technology based on user feedback to improve its usability and accessibility.
Also in 7 other lessons
Middle School · Computing & Society
Compare the roles of individuals, communities, organizations, and governments in shaping computing technologies across major eras in computing history.
Only this lesson so far
Specialty I · Software Development
Create software that serves intended users and contexts using human-centered design principles.
Also in 3 other lessons
High School · Computing & Society
Analyze the historical trajectory of a specific computing technology and how its development is linked to societal and environmental factors.
Also in 1 other lesson
Explore
How a menu is built
In Quorum, every part of a menu is a MenuItem. A menu item on its own is a
command. When you add other items to it, it becomes a menu that
holds them. This means a menu is built like a tree:
- the
MenuBarholds the top-level items, such as Animals and Help; - each top-level item holds the commands or submenus in its menu;
- a submenu, such as Pets, holds its own commands.
In this tutorial you will build this menu:
- Animals
- Pets: Dog, Cat
- Farm: Cow, Duck
- Help
- About
A behavior for the commands
Each command needs something to do. Every command in this program shows a
message, so one behavior class can serve all of them. Add a new class named
OutputBehavior. It remembers a label and a message, and when it runs, it puts
the message on the label and writes it to the output:
use Libraries.Game.Graphics.Label
use Libraries.Interface.Behaviors.Behavior
use Libraries.Interface.Events.BehaviorEvent
class OutputBehavior is Behavior
Label label = undefined
text message = ""
action Run(BehaviorEvent event)
if label not= undefined
label:SetText(message)
end
output message
end
action SetLabel(Label newLabel)
label = newLabel
end
action SetMessage(text newMessage)
message = newMessage
end
end
Putting a menu bar on a page
A page has an Add action for each common control, like AddButton and
AddLabel. There is no AddMenuBar. Instead, you make the menu bar
yourself and give it to the page's plain Add action. The page puts it in
a row of its own, like everything else, and stretches it across the full
width of the window. Add the menu bar first, so it sits at the top of
the page, above everything else.
Now replace Main.quorum. The MakeCommand action builds one command,
gives it an OutputBehavior, and returns it, so each command takes one line.
use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Game.Graphics.Label
use Libraries.Interface.Controls.MenuBar
use Libraries.Interface.Controls.MenuItem
class Main
Label message = undefined
action Main
Form form
Page page = form:GetMainPage()
MenuBar bar
bar:SetName("Main menu")
bar:SetDescription("Use the arrow keys to open the Animals and Help menus")
page:Add(bar)
message = page:AddLabel("Message", "Choose an animal from the Animals menu.")
MenuItem animals
animals:SetName("Animals")
animals:SetMnemonic("a")
bar:Add(animals)
MenuItem pets
pets:SetName("Pets")
animals:Add(pets)
pets:Add(MakeCommand("Dog", "Woof!"))
pets:Add(MakeCommand("Cat", "Meow!"))
MenuItem farm
farm:SetName("Farm")
animals:Add(farm)
farm:Add(MakeCommand("Cow", "Moo!"))
farm:Add(MakeCommand("Duck", "Quack!"))
MenuItem help
help:SetName("Help")
help:SetMnemonic("h")
bar:Add(help)
help:Add(MakeCommand("About", "This program shows how menus work."))
form:Display()
end
action MakeCommand(text name, text sound) returns MenuItem
OutputBehavior behavior
behavior:SetLabel(message)
behavior:SetMessage(sound)
MenuItem item
item:SetName(name)
item:SetBehavior(behavior)
return item
end
end
Some things to notice:
page:Add(bar)comes first. A page shows its controls from top to bottom in the order you add them, so the menu bar is the top row and the message is below it.AddLabelgives back the label. The program keeps it inmessage, so everyOutputBehaviorcan change its text.- Build from the top down. Add a menu to its parent before you add
items to it. For example,
animals:Add(pets)comes beforepets:Add(...). A submenu shows its small arrow only if it already has a parent menu when its first item is added. SetNameis the text on the item. It is also what a screen reader reads, so keep it short and clear.- Only commands get behaviors. Animals, Pets and Farm only open menus, so they have no behavior.
- The form sets the Tab order for you. The menu bar can take the keyboard focus, so it is part of the page's Tab order, just like a button. Because it is first on the page, it has the focus when the app starts. Press Tab to move to the message, and Shift+Tab to move back.
Using a menu with the keyboard
Every Quorum menu works with the keyboard. When the menu bar first gets the focus, nothing on it is chosen yet. Press Right to choose the first menu, or press a menu's mnemonic letter. Then use these keys:
| Key | What it does |
|---|---|
| Right arrow | Moves to the next menu on the menu bar. On an item that holds a submenu it opens the submenu. |
| Left arrow | Moves to the previous menu on the menu bar or closes a submenu. |
| Down arrow | Opens the chosen menu or moves down one item. |
| Up arrow | Opens the chosen menu or moves up one item. |
| Enter or Space | Runs the chosen command or opens the chosen menu. |
| Esc | Closes the open submenu or menu. Pressed again it gives the focus back to where it was before the menu bar. |
| A letter | Chooses the item whose mnemonic is that letter. |
When you choose a command, the menu closes and the keyboard focus goes back to the control that had it before you moved to the menu bar.
A screen reader reads each item's name as you move, so every item needs a clear name.
Notice that no single key jumps to the menu bar from somewhere else on the page. In a form, you reach it with Tab and Shift+Tab, like any other control. Putting it first on the page keeps it easy to find.
Mnemonics
A mnemonic is one letter that chooses a menu item from the keyboard.
In Main, animals:SetMnemonic("a") means that pressing A while the
menu bar has the focus opens the Animals menu and chooses its first item.
Mnemonics work for the items of an open menu too, so you could give Pets
the mnemonic p and Farm the mnemonic f. Pick letters that are in the
item's name, and do not use the same letter twice in one menu.
Keyboard shortcuts
A shortcut runs a command without opening any menu. Shortcuts are made
from an InputSet, which describes a key and any modifier keys held with
it. You only need four lines of it here: KeyboardEvent holds a constant
for every key, such as keys:D, SetKeyboardInput chooses the key, and
AddModifier adds a key that must be held with it, such as CONTROL. The
Input Sets tutorial covers input sets in
full.
There is one catch. A shortcut is added to the running app's list of keys,
so it can only be set once the app's window is open. The lines in
action Main run before form:Display() opens the window. To set a
shortcut after the window opens, the program itself becomes a Form: the
class says is Form, and its CreateGame action runs once the window is
open. CreateGame first calls parent:Form:CreateGame(), so the form still
does all of its own setup, then sets the shortcut.
Here is the whole program again, now with the shortcut Ctrl+D for Dog:
use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Game.Graphics.Label
use Libraries.Game.InputSet
use Libraries.Interface.Events.KeyboardEvent
use Libraries.Interface.Controls.MenuBar
use Libraries.Interface.Controls.MenuItem
class Main is Form
Label message = undefined
MenuItem dog = undefined
action Main
Page page = GetMainPage()
MenuBar bar
bar:SetName("Main menu")
bar:SetDescription("Use the arrow keys to open the Animals and Help menus")
page:Add(bar)
message = page:AddLabel("Message", "Choose an animal from the Animals menu.")
MenuItem animals
animals:SetName("Animals")
animals:SetMnemonic("a")
bar:Add(animals)
MenuItem pets
pets:SetName("Pets")
animals:Add(pets)
dog = MakeCommand("Dog", "Woof!")
pets:Add(dog)
pets:Add(MakeCommand("Cat", "Meow!"))
MenuItem farm
farm:SetName("Farm")
animals:Add(farm)
farm:Add(MakeCommand("Cow", "Moo!"))
farm:Add(MakeCommand("Duck", "Quack!"))
MenuItem help
help:SetName("Help")
help:SetMnemonic("h")
bar:Add(help)
help:Add(MakeCommand("About", "This program shows how menus work."))
Display()
end
action CreateGame
parent:Form:CreateGame()
KeyboardEvent keys
InputSet dogKeys
dogKeys:SetKeyboardInput(keys:D)
dogKeys:AddModifier(dogKeys:CONTROL)
dog:SetShortcut(dogKeys)
end
action MakeCommand(text name, text sound) returns MenuItem
OutputBehavior behavior
behavior:SetLabel(message)
behavior:SetMessage(sound)
MenuItem item
item:SetName(name)
item:SetBehavior(behavior)
return item
end
end
What changed:
class Main is FormmeansMainis the form. So it callsGetMainPage()andDisplay()itself, with noform:in front.- Dog is kept in the
dogvariable soCreateGamecan reach it. CreateGamesets the shortcut once the window is open.
The menu now shows "Control + D" next to Dog, and pressing Ctrl+D runs the Dog command even when the menu is closed. Shortcuts help everyone work faster, and they matter most to people who use only a keyboard.
Finding the path to a command
Sometimes a behavior needs to know which menu item ran it. The
BehaviorEvent that Run gets holds that item, and a menu item's
GetMenuPath action returns every item from the top of the menu down to
it. For Dog, the path is Animals, Pets, Dog. Here is OutputBehavior again,
now writing the path to the output as well:
use Libraries.Containers.Array
use Libraries.Game.Graphics.Label
use Libraries.Interface.Behaviors.Behavior
use Libraries.Interface.Controls.MenuItem
use Libraries.Interface.Events.BehaviorEvent
class OutputBehavior is Behavior
Label label = undefined
text message = ""
action Run(BehaviorEvent event)
if label not= undefined
label:SetText(message)
end
output message
if event:GetItem() is MenuItem
MenuItem item = cast(MenuItem, event:GetItem())
Array<MenuItem> path = item:GetMenuPath()
text route = ""
integer i = 0
repeat while i < path:GetSize()
if i > 0
route = route + " > "
end
route = route + path:Get(i):GetName()
i = i + 1
end
output "You chose " + route
end
end
action SetLabel(Label newLabel)
label = newLabel
end
action SetMessage(text newMessage)
message = newMessage
end
end
The is MenuItem check matters. A behavior can be used by other controls
too, such as a button, and then the item is not a menu item. You will use
OutputBehavior with buttons in the Toolbars
tutorial.
Where menus came from
Menus and windows were shaped by people, companies and a government. In 1968, Douglas Engelbart and his team at the Stanford Research Institute showed NLS, a system with a mouse and windows on the screen. Its funding came from DARPA, a research agency of the US government, and the US Air Force (source). In the 1970s, researchers at Xerox's Palo Alto Research Center (PARC) built the Alto, a computer used through windows, icons, menus and a mouse. In 1979, a group from Apple led by Steve Jobs visited PARC. Apple's Lisa, released in 1983, had a menu bar of pull-down menus at the top of the screen, and the Macintosh followed in 1984. Microsoft first released Windows in 1985 (source).
Engage
In this activity, you and a partner design and build the menu bar for a
simple drawing program, and test how quickly it can be used. The goal is to organize commands into menus, and to choose
mnemonics and shortcuts that make common commands fast. Plan it first, on
paper or with physical manipulatives such as sticky notes or index cards: decide on three menus, the commands in each, at least one submenu, a
mnemonic for each top-level menu, and two shortcuts. Then build it in
Quorum on a page, using OutputBehavior so each command writes what it would
do.
When it works, swap programs. Your partner runs every command with the keyboard, the mouse or another device they use, and says out loud how they reached each one. Find the command your partner used most, and count the steps it took, such as key presses, clicks or taps. Could a shortcut, or moving it to a different menu, make it shorter? Ask your partner which command was the hardest to find. Change one thing in your design based on what your partner told you and what you counted.
Wrap up
Menus hide commands until you ask for them. That saves space but makes commands harder to discover. How do mnemonics and shortcuts change that trade-off? Which commands in your own program deserve a shortcut, and why? In "Where menus came from", what did the government do that a company could not, and what did the companies do that the researchers did not?
The next tutorial covers Popup Menus, menus that appear right where you need them instead of on a bar.