Input Sets

Overview

Every program reacts to input. A person presses a key, clicks the mouse or taps a screen, and something happens. Quorum already knows what most keys should do in its controls: Tab moves the focus, and Space presses a button. An input set describes one kind of input, such as "the Space key is pressed". An input table connects input sets to the behaviors that should run. In this tutorial, you will use input sets and input tables to add your own keys to a game, give different items different keys, and choose exactly when a behavior runs. You will also learn how to add keys without taking away the keys that keyboard and screen reader users depend on.

Goals

You have the following goals for this tutorial:

By the end of this lesson, you can

  • Create an InputSet for a key or mouse button and connect it to a behavior with an InputTable
  • Use input groups so that different items respond to the same key in different ways
  • Use triggers, modifier keys and event counts to control when a behavior runs
  • Add new keys without breaking the keys people already use to move around a program

Warm up

Think of a game or app you use with a keyboard. Which keys does it use, and how did you learn them? Does the same key do different things in different places, like Enter in a text box and Enter on a button? If you could change one key in that program, which would it be and why? Who might need to change a key for a reason you have not thought of?

Vocabulary

You will be learning about the following vocabulary words:

Vocabulary
TermDefinition
Input setA description of one kind of input, such as the Space key being pressed or the left mouse button being clicked.
Input tableA list that connects input sets to the behaviors that should run when that input happens.
Default input tableThe input table every game starts with. It holds keys that work everywhere, such as Tab to move the focus.
Input groupA short piece of text stored on an item. It tells the game which input table to use when that item has the focus.
FocusThe item that keyboard input goes to right now. Only one item has the focus at a time.
TriggerThe moment a behavior runs: when input begins, while it continues or when it finishes.
Modifier keyA key held down with another key or a click, such as Shift, Ctrl, Alt or Command.
Event countHow many times input must happen quickly in a row, such as 2 for a double-click.

New Code to Learn

You will be using the following new pieces of code:

New Code to Learn
Quorum CodeCode ExampleExplanation
InputSetInputSet spaceSetCreates a new input set. It describes no input until you set one.
SetKeyboardInput(integer key)spaceSet:SetKeyboardInput(keys:SPACE)Makes the input set describe a key on the keyboard. The keys are constants in KeyboardEvent.
SetMouseInput(integer button)clickSet:SetMouseInput(mouse:LEFT)Makes the input set describe a mouse button. The buttons are constants in MouseEvent.
InputTableInputTable squareTableCreates a new input table with nothing in it.
Add(InputSet set, Behavior behavior)squareTable:Add(spaceSet, rotate)Runs the behavior whenever the input in the input set happens.
SetIdentifier(text id)squareTable:SetIdentifier("Square")Names the input table. Items whose input group matches this name use the table.
GetDefaultInputTable()InputTable table = GetDefaultInputTable()Returns the game's default input table.
AddInputTable(InputTable table)AddInputTable(squareTable)Adds an input table to the game so it can be used.
SetInputGroup(text group)square:SetInputGroup("Square")Sets which input table an item uses when it has the focus.
SetInputTrigger(integer trigger)holdSet:SetInputTrigger(holdSet:CONTINUE)Chooses when the behavior runs: BEGIN, CONTINUE, FINISH or FINISH_UNCAUGHT.
AddModifier(integer modifier)shiftSet:AddModifier(shiftSet:SHIFT)Requires a modifier key, such as SHIFT, CONTROL, ALT or META, to be held down.
SetEventCount(integer count)doubleClick:SetEventCount(2)Requires the input to happen this many times in a row.

CSTA Standards

This tutorial covers the following CSTA standards:

For teachers · 2026 CSTA K–12 Computer Science Standards

HS-PRO-PD-12

High School · Programming

Create a modular program that uses procedures, external libraries, or objects to improve reusability and readability.

Also in 9 other lessons

MS-PRO-PD-12

Middle School · Programming

Use a procedure to structure code for clarity and reusability.

Also in 5 other lessons

S1-SWD-TR-07

Specialty I · Software Development

Design a test plan that exercises program functionality, including edge cases, error conditions, and varied user inputs.

Also in 2 other lessons

MS-ALG-PS-02

Middle School · Algorithms & Design

Model a given algorithm with a flowchart or pseudocode that includes a combination of control structures and procedures.

Also in 1 other lesson

Every standard, and the lessons that cover it

Explore

How Quorum decides what a key does

When you press a key, Quorum looks at the item that has the focus, which you learned about in the Focus tutorial. Quorum then checks input tables in this order:

  1. The input table whose name matches the focused item's input group.
  2. If that table has nothing for this key, the input tables of the item's parent, then its parent's parent, and so on.
  3. If none of those has anything for this key, the default input table.

As soon as one table runs a behavior, Quorum stops looking. This is why the same key can do different things in different places. Quorum's own controls work this way. Every Button has the input group "Button", and the "Button" table runs the button's behavior when you press Space or Enter. The default input table is where Tab and Shift+Tab live: they move the focus to the next or previous item in every game. It also holds Shift+F1, which shows the tooltip of the control that has the focus, and Escape, which closes it.

The programs in this tutorial use the Game class rather than a form. The controls on a form already come with their keys, and input tables are most useful when you draw your own items, as games do. You will make two shapes and give them keys of their own.

A behavior is the code that runs. You may remember behaviors from the Behaviors tutorial. Every behavior has a Run action, and the BehaviorEvent it receives tells you which item had the focus.

A behavior to run

Your first behavior writes out the name of the focused item. Add a new class named OutputBehavior.quorum:

use Libraries.Interface.Behaviors.Behavior
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Item

class OutputBehavior is Behavior
    action Run(BehaviorEvent event)
        Item item = event:GetItem()

        // There may be no focused item, so check first.
        if item = undefined
            output "No item has the focus."
        else
            output item:GetName() + " has the focus."
        end
    end
end

Adding a key to the default input table

Now make the game in Main.quorum. It has two shapes, an orange square and a purple circle. Each shape gets a name and a description, so a screen reader can say what it is. Shapes are not read by screen readers unless you give them an accessibility code, so each shape also gets the code ITEM. Each shape can take the focus, and Tab moves between them.

The new part is at the end. The program makes an input set for the Space key, gets the default input table, and adds the input set with an OutputBehavior.

use Libraries.Game.Game
use Libraries.Game.InputSet
use Libraries.Game.InputTable
use Libraries.Interface.Events.KeyboardEvent
use Libraries.Game.Graphics.Drawable
use Libraries.Game.Graphics.Color
use Libraries.Interface.Item

class Main is Game
    KeyboardEvent keys
    Color color
    Drawable square
    Drawable circle

    action Main
        StartGame()
    end

    action CreateGame
        square:LoadFilledRectangle(100, 100, color:Orange())
        square:SetName("Orange square")
        square:SetDescription("Press Space to hear which shape has the focus.")
        square:SetAccessibilityCode(square:parent:Item:ITEM)
        square:SetFocusable(true)
        square:SetPosition(200, 250)
        Add(square)

        circle:LoadFilledCircle(50, color:Purple())
        circle:SetName("Purple circle")
        circle:SetDescription("Press Space to hear which shape has the focus.")
        circle:SetAccessibilityCode(circle:parent:Item:ITEM)
        circle:SetFocusable(true)
        circle:SetPosition(500, 250)
        Add(circle)

        // Tab and Shift+Tab move between the two shapes.
        square:SetNextFocus(circle)
        square:SetPreviousFocus(circle)
        circle:SetNextFocus(square)
        circle:SetPreviousFocus(square)
        SetFocus(square)

        // Run OutputBehavior whenever Space is pressed.
        InputSet spaceSet
        spaceSet:SetKeyboardInput(keys:SPACE)

        InputTable defaultTable = GetDefaultInputTable()
        OutputBehavior outputBehavior
        defaultTable:Add(spaceSet, outputBehavior)
    end
end

Run the program and press Space. The output says "Orange square has the focus." Press Tab, then Space again, and the output names the circle. With a screen reader running, Tab also makes the screen reader say the shape's name.

KeyboardEvent holds a constant for every key, such as keys:SPACE, keys:ENTER and keys:R. ITEM is a constant of the Item class, which a Drawable inherits from, so the program reaches it through square:parent:Item:ITEM.

Giving each item its own keys

The default input table is the same for every item. Often you want a key to do something different depending on what has the focus. In this part, the square turns when you press Space, and the circle grows or shrinks when you press Enter.

Add two more behaviors to your project. The first turns the focused item by 45 degrees. Put it in RotationBehavior.quorum:

use Libraries.Interface.Behaviors.Behavior
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Item
use Libraries.Interface.Item2D

class RotationBehavior is Behavior
    action Run(BehaviorEvent event)
        Item item = event:GetItem()

        if item = undefined
            output "Nothing turned, because no item has the focus."
        elseif item is Item2D
            // A Drawable is a kind of Item2D, which can turn.
            Item2D item2D = cast(Item2D, item)
            item2D:Rotate(45)
            output item2D:GetName() + " turned to " + item2D:GetRotation() + " degrees."
        end
    end
end

The second makes the item bigger, then back to normal, each time it runs. Put it in ScaleBehavior.quorum:

use Libraries.Interface.Behaviors.Behavior
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Item
use Libraries.Interface.Item2D

class ScaleBehavior is Behavior
    boolean scaledUp = false

    action Run(BehaviorEvent event)
        Item item = event:GetItem()

        if item = undefined
            output "Nothing changed size, because no item has the focus."
        elseif item is Item2D
            Item2D item2D = cast(Item2D, item)
            if scaledUp
                item2D:SetScaleFromCenter(1.0)
            else
                item2D:SetScaleFromCenter(1.5)
            end
            scaledUp = not scaledUp
            output item2D:GetName() + " is now " + item2D:GetScaleX() + " times its normal size."
        end
    end
end

Now connect them. Three steps do it:

  1. Give each shape an input group with SetInputGroup.
  2. Make an input table for each group, and name it with SetIdentifier. The name must match the input group exactly.
  3. Add each table to the game with AddInputTable. The default input table is already part of the game, but new tables must be added.

Here is the whole Main.quorum. The descriptions now tell a person which key to press:

use Libraries.Game.Game
use Libraries.Game.InputSet
use Libraries.Game.InputTable
use Libraries.Interface.Events.KeyboardEvent
use Libraries.Game.Graphics.Drawable
use Libraries.Game.Graphics.Color
use Libraries.Interface.Item

class Main is Game
    KeyboardEvent keys
    Color color
    Drawable square
    Drawable circle

    action Main
        StartGame()
    end

    action CreateGame
        square:LoadFilledRectangle(100, 100, color:Orange())
        square:SetName("Orange square")
        square:SetDescription("Press Space to turn the square.")
        square:SetAccessibilityCode(square:parent:Item:ITEM)
        square:SetFocusable(true)
        square:SetPosition(200, 250)
        Add(square)

        circle:LoadFilledCircle(50, color:Purple())
        circle:SetName("Purple circle")
        circle:SetDescription("Press Enter to make the circle bigger or smaller.")
        circle:SetAccessibilityCode(circle:parent:Item:ITEM)
        circle:SetFocusable(true)
        circle:SetPosition(500, 250)
        Add(circle)

        square:SetNextFocus(circle)
        square:SetPreviousFocus(circle)
        circle:SetNextFocus(square)
        circle:SetPreviousFocus(square)
        SetFocus(square)

        // Space anywhere: say which item has the focus.
        InputSet spaceSet
        spaceSet:SetKeyboardInput(keys:SPACE)
        OutputBehavior outputBehavior
        InputTable defaultTable = GetDefaultInputTable()
        defaultTable:Add(spaceSet, outputBehavior)

        // Step 1: give each shape an input group.
        square:SetInputGroup("Square")
        circle:SetInputGroup("Circle")

        // Step 2: make a table for each group, with a matching name.
        InputSet enterSet
        enterSet:SetKeyboardInput(keys:ENTER)

        RotationBehavior rotationBehavior
        InputTable squareTable
        squareTable:SetIdentifier("Square")
        squareTable:Add(spaceSet, rotationBehavior)

        ScaleBehavior scaleBehavior
        InputTable circleTable
        circleTable:SetIdentifier("Circle")
        circleTable:Add(enterSet, scaleBehavior)

        // Step 3: add the new tables to the game.
        AddInputTable(squareTable)
        AddInputTable(circleTable)
    end
end
Blocks

Which table wins?

Try these keys and compare what happens with the order from the start of this tutorial:

  • Space on the square: the square turns. The "Square" table has Space, so Quorum stops there. OutputBehavior does not run.
  • Space on the circle: the output names the circle. The "Circle" table has no Space, so Quorum falls back to the default input table.
  • Enter on the circle: the circle grows, or shrinks back.
  • Enter on the square: nothing happens. No table has Enter for the square.
  • Tab: the focus moves. Neither new table has Tab, so the default input table still handles it.

One behavior, more than one key

An input table can hold many input sets, and two input sets can run the same behavior. This is how a program offers a second key for the same job, or lets a person choose their own keys. Add these lines to CreateGame, and the R key also turns the square:

InputSet rSet
rSet:SetKeyboardInput(keys:R)
squareTable:Add(rSet, rotationBehavior)

To take a key away, use the table's RemoveKey action with an input set for that key.

Triggers: when a behavior runs

Input has a beginning, a middle and an end. A key goes down, is held, and comes back up. The trigger chooses which of these moments runs the behavior. The triggers are constants in InputSet:

  • BEGIN: when the input starts, such as when a key is first pressed. This is the trigger an input set uses if you do not choose one.
  • CONTINUE: on every frame while the input goes on, such as while a key is held down. This is useful for moving a character for as long as an arrow key is held.
  • FINISH: when the input ends, such as when a key is let go.
  • FINISH_UNCAUGHT: for keys only. When a key is let go, but only if it was not used as part of a key combination.

Use SetInputTrigger to choose one. Add these lines to CreateGame. Now the square keeps spinning for as long as the Right Arrow key is held down:

InputSet holdRight
holdRight:SetKeyboardInput(keys:RIGHT)
holdRight:SetInputTrigger(holdRight:CONTINUE)
squareTable:Add(holdRight, rotationBehavior)

Modifier keys

A modifier key is held down while you press another key or click. The modifiers are constants in InputSet:

  • SHIFT;
  • CONTROL, the Ctrl key;
  • ALT, which is the Option key on a Mac;
  • META, the Command key on a Mac or the Windows key on Windows. Windows keeps most Windows key combinations for itself, so avoid META there.

Use AddModifier to require one, and RemoveModifier to take it away. This input set runs a behavior on Ctrl+click with the left mouse button:

use Libraries.Game.InputSet
use Libraries.Interface.Events.MouseEvent

MouseEvent mouse
InputSet controlClick
controlClick:SetMouseInput(mouse:LEFT)
controlClick:AddModifier(controlClick:CONTROL)

A mouse input set needs a key that does the same thing, so it works from the keyboard too. When you add controlClick to a table, add this input set for Ctrl+Enter to the same table, with the same behavior:

use Libraries.Game.InputSet
use Libraries.Interface.Events.KeyboardEvent

KeyboardEvent keys
InputSet controlEnter
controlEnter:SetKeyboardInput(keys:ENTER)
controlEnter:AddModifier(controlEnter:CONTROL)

An input set with a modifier is a different input set from one without it. Shift+Space and Space can run different behaviors, even in the same table.

Double-clicks and event counts

Some input only counts when it happens quickly more than once, like a double-click. Use SetEventCount to say how many times. This input set describes a double-click with the left mouse button:

use Libraries.Game.InputSet
use Libraries.Interface.Events.MouseEvent

MouseEvent mouse
InputSet doubleClick
doubleClick:SetMouseInput(mouse:LEFT)
doubleClick:SetEventCount(2)

The count can be from 1 to 4. As with Ctrl+click, a double-click needs a key too. Add doubleClick and an input set for a key such as Enter to the same table with the same behavior, as in "One behavior, more than one key".

Keys that everyone can use

Input sets give you a lot of power over keys. Use it with care:

  • Leave Tab and Shift+Tab alone. Keyboard and screen reader users move through a program with them. If you add Tab to a table, that item may become a keyboard trap: the focus goes in and cannot get out.
  • Give every mouse action a key too. Some people cannot use a mouse. If a double-click opens something, a key such as Enter should open it as well.
  • Say which keys to use. Put the key in the item's description, as the program above does. A screen reader reads the description, and you can show the same words on screen.
  • Avoid keys Quorum already uses. Tab, Shift+Tab, Shift+F1 and Escape are in the default input table. If you add one of them to that table, your behavior takes its place in every part of the program.
  • Avoid keys the computer already uses. Screen readers use many keys for themselves. For example, NVDA uses Insert and VoiceOver uses Ctrl+Option.

You can practice moving through programs without a mouse in No-Mouse Practice.

Engage

In this activity, you design a new shape with its own keys for the game on this page, write a test plan for it before you try it, and then run the plan. The goal is to set up input groups and input tables, and to plan tests before trying a program. Add a third shape to the game, such as a filled rectangle of a different color. Give it a name, a description, the ITEM accessibility code, and a place in the Tab order. Then give the new shape its own input group and input table, with two keys. At least one key must use a modifier, such as Shift+R to turn the other way with Rotate(-45).

Before you run the program, write a short test plan: for each shape, list each key, what should happen, and what should not happen, including keys that should do nothing. Then run the plan without touching the mouse, and mark each line "pass" or "fail". Fix any failures and run the plan again. Finally, add one change that lets a person use a second key for the same job, and add lines to your plan to test it. Then draw "Which table wins?" for one of your new keys as a flowchart or write it as pseudocode: which input table Quorum checks first, what happens when a key matches, and what happens when it doesn't. You may also be given a Parsons problem that asks you to put the lines of an input table setup in the right order.

Wrap up

Input sets and input tables let you decide what each key does, and where. Go back to your test plan. Which test found a problem you did not expect? If a person told you they could not press two keys at once, how would you change your program for them?