Events
Events are "things" that happen during a program's execution, like a mouse click, or keypress. We can write code that runs when that event occurs.
We could first write a function that is intended to run when an event
occurs. These functions are referred to as an Event Handlers. In Kid.js
we can assoicate an event handler with an event using the on()
method.
We can also write an "inline" function and pass it as the second parameter to the on() method. Inline functions don't have a name and are sometimes referred to as annonymous functions.
In Kid.js you can also pass a single statement.
Triggers
In addition to events, Kid.js also supports a concept we refer to as
"triggers". If we pass an expression as the first parameter to the
on()
method, Kid.js will watch for this expression to be
true and execute the event handler when it is.
Keyboard Input
There are a number of events related to keyboard input. When any of these events happen, the specific key pressed is passed as a parameter to the event handler.
keydown
The keydown event is triggered when the user pressed down on a key.
keyup
The keyup event is triggered when the key is released.
Kid.js also triggers an event matching the name of the key pressed. This makes it easy to attach code to a specific key press.
Mouse Input
At anytime, the global variables mouseX
and mouseY
contain the x and y coordinates of the mouse.
There are also a number of events related to mouse input. When any of these events happen, the specific x and y coordinates of the mouse area passed as parameters to the event hander.
click
The click event is triggered when the user clicks the mouse, or on a smartphone or tablet, taps the display.
doubleclick
The doubleclick event is triggered when the user clicks or taps twice.
mousedown
The mousedown event is triggered when the user presses down on the mouse, or begins to tap.
mouseup
The mouseup
event is triggered when the user releases
the mouse button, or removes their finger.
mousemove
The mousemove
event is triggered whenever the user
moves the mouse cursor across the app.
Device Orientation
Kid.js triggers a number of events based on the orientation of the user's device. These events only occur on smartphones, tablets or other devices with a gyroscope. The user must also first tap the app before these events can be detected.
tiltleft
The tiltleft
event is triggered when the user tilts the
device more than 10 degrees to the left.
tiltright
The tiltright
event is triggered when the user tilts
the device more than 10 degrees to the right.
tiltup
The tiltup
event is triggered when the user tilts the
device more than 10 degrees forward.
tiltdown
The tiltdown
event is triggered when the user tilts the
device more than 10 degrees backward.
Device Motion
Kid.js triggers a number of events based on the motion of the user's device. These events only occur on smartphones, tablets or other devices with a gyroscope. The user must also first tap the app before these events can be detected.
shake
The shake
event is triggered when the user shakes their
device.