Events
An event is something that happens - a click, a change, a window closing. You say what should happen with when.
Otter
create window into app
create button into saveButton
text of saveButton is "Save"
put saveButton in app
when saveButton is clicked
say "Saved!"
.
show app
The block runs every time the event happens. Inside it you can do anything an Otter program can do: say things, change other controls, read text boxes.
The events
Output
clicked button, primary button, secondary button, danger button, card
changed text box: each time its text changes
closed window: when the person closes it
Reacting to typing
Otter
create window into app
create text box into input
create text into echo
put input in app
put echo in app
when input is changed
text of echo is text of input
.
show app
Several controls
Give each control its own when. They can share variables, so one click can change what another control shows.
Controls made in a loop
A when set up inside a loop remembers the pass that set it up. Each button below shows its own number:
Otter
create window into app
create text into shown
put shown in app
numbers are empty
add 1 to numbers
add 2 to numbers
add 3 to numbers
for each n in numbers
create button into b
text of b is n
put b in app
when b clicked
text of shown is n
.
.
The loop variable, and anything first set inside the loop, belong to that pass. A name that already existed before the loop, such as a counter, is shared: every button's when sees its latest value.
In web applications
The same words work in programs compiled with otter web: when button is clicked and when box is changed. Web pages add drag and drop events - see Web applications.
Making controls while the program runs
A handler can create controls too. Each create makes a new control, so this list gets one more line every time the button is clicked. It works the same in Windows applications and in web applications.
Otter
create window into app
create text box into taskInput
create button into addButton
text of addButton is "Add"
create column into taskList
put taskInput in app
put addButton in app
put taskList in app
when addButton is clicked
task is text of taskInput
if task is not ""
create text into item
item has text task
put item in taskList
text of taskInput is ""
.
.
show app
item names the newest line each time the handler runs; the lines added earlier stay where they were put. Putting something that is not a control, such as a number, stops the program with a message that says so.
hide, focus and clear are not part of Otter 1.0. They are planned for Otter 1.1.
How handlers run
Handlers run one at a time. A handler finishes before the next one starts, and it never interrupts other Otter code, so handlers and the rest of the program can share variables safely.
Events from one control or connection arrive in order: two clicks run the handler twice, in the order they happened. Events from different sources can be handled in any order.
In a console or Windows program, handlers run once the program reaches its end, and during wait. In a web page, the browser runs them whenever the event happens, including while the program is waiting for something such as a web request.
Keyboard events and file events are not part of the Windows event set yet.
Controls
Web applications