Demo window 2. Getting user input

For almost all applications, you will want the user (or the participant in an experiment) to be able to click on things in the Demo window, or to control the Demo window by pressing keys. Here is a presentation with two screens:

demo Erase all
demo Select inner viewport: 0, 100, 0, 100
demo Axes: 0, 100, 0, 100
demo Paint rectangle: “purple”, 0, 100, 0, 100
demo Pink
demo Text: 50, “centre”, 50, “half”, “This is the first page”
demoWaitForInput ()
demo Erase all
demo Paint rectangle: “purple”, 0, 100, 0, 100
demo Text: 50, “centre”, 50, “half”, “This is the second page”

In this example, you go from the first to the second screen either by clicking with the mouse or by pressing any key. You will usually want to be more selective in your choice of user actions to respond to. The function demoWaitForInput always returns 1, so that you can use it nicely in a loop, in which you can react selectively:

label FIRST_SCREEN
demo Erase all
demo Black
demo Times
demo 24
demo Select inner viewport: 0, 100, 0, 100
demo Axes: 0, 100, 0, 100
demo Paint rectangle: “purple”, 0, 100, 0, 100
demo Pink
demo Text: 50, “centre”, 50, “half”, “This is the first page”
while demoWaitForInput ()
if demoClicked ()
goto SECOND_SCREEN
elsif demoKeyPressed ()
if demoKey$ () = “→” or demoKey$ () = “ ”
goto SECOND_SCREEN
endif
endif
endwhile
label SECOND_SCREEN
demo Erase all
demo Paint rectangle: “purple”, 0, 100, 0, 100
demo Text: 50, “centre”, 50, “half”, “This is the second page”
while demoWaitForInput ()
if demoClicked ()
goto END
elsif demoKeyPressed ()
if demoKey$ () = “←”
goto FIRST_SCREEN
elsif demoKey$ () = “→” or demoKey$ () = “ ”
goto END
endif
endif
endwhile
label END

This script allows you to use the arrow keys and the space bar to navigate between the two screens. A shorter version is:

label FIRST_SCREEN
demo Erase all
demo Black
demo Times
demo 24
demo Select inner viewport: 0, 100, 0, 100
demo Axes: 0, 100, 0, 100
demo Paint rectangle: \"purple\", 0, 100, 0, 100
demo Pink
demo Text: 50, \"centre\", 50, \"half\", \"This is the first page\"
while demoWaitForInput ()
goto SECOND_SCREEN demoInput (“•→ ”)
endwhile
label SECOND_SCREEN
demo Erase all
demo Paint rectangle: \"purple\", 0, 100, 0, 100
demo Text: 50, \"centre\", 50, \"half\", \"This is the second page\"
while demoWaitForInput ()
goto END demoInput (“•→ ”)
goto FIRST_SCREEN demoInput (“←”)
endwhile
label END

This uses two tricks, namely the possibility of following the goto statement by a condition and using demoInput to quickly test for multiple possible inputs (the bullet represents a mouse click).

Links to this page


© Paul Boersma 2009–2023