Handling events
A handler holds statements, or a function to call. on_click: $save calls the function save:
<KScript.Window title="Notes">
<let name="saved" value="0" />
<fn name="save">
<set name="saved" op="+=" value="1" />
</fn>
<Button on_click="$save">Save</Button>
<Text>Saved {$saved} times</Text>
</KScript.Window>import KScript.UI
Window(title: 'Notes') {
$saved = 0
fn save() {
$saved += 1
}
Button(on_click: $save) "Save"
Text "Saved {$saved} times"
}A button or box also takes the handler shorthands directly, such as Button(toggle: $open) "Details". @close_window() in a handler closes the window.
If a handler fails, the error is shown at the top of the window and the window carries on.
Timers
Timer runs its on_tick handler on a timer, while when is true:
<KScript.Window title="Stopwatch">
<let name="seconds" value="0" />
<let name="running" value="false" />
<Timer every="1s" when="$running" on_tick="$seconds += 1" />
<Text size="32">{$seconds}s</Text>
<Button toggle="$running">Start or stop</Button>
</KScript.Window>import KScript.UI
Window(title: 'Stopwatch') {
$seconds = 0
$running = false
Timer(every: 1s, when: $running, on_tick: $seconds += 1)
Text(size: 32) "{$seconds}s"
Button(toggle: $running) "Start or stop"
}