Spork Mouse Events

Spork provides easy access to handling mouse and touch events. Whenever the mouse interacts with the Spork canvas or when the canvas is touched, the functions below are executed. To handle mouse and touch events, we need only define these functions in our programs. When each of these functions is executed, the variables mouseX and mouseY are set to the canvas coordinates of the mouse (or touch). When the mouse button is pressed (or the canvas is touched), the variable mouseDown is set to true. When the button is released (or the touch ends) this variable is set to false. Follow the links to see simple examples of each function in action.
  1. onclick() This function is executed whenever a mouse button is clicked in the canvas or the canvas is tapped.
  2. onmousemove() This function is executed when the mouse or a touch is moved while above the canvas.
  3. mouseDown This variable is true when a mouse button is pressed or when the canvas is being touched. Otherwise it is false.

Key Events

Spork Key Events

Spork provides easy access to handling key events. Whenever a key is pressed three events are "fired."
  1. As the key goes down, a "keydown" event happens. This event fires repeatedly as long as the key is down.
  2. When the key is down, a "keypress" event happens.
  3. When the key is released, a "keyup" event happens.
When a key is pressed, several variables are set. After these variables are set, the function onkeydown() is called. To have spork respond to key presses, you should define this function. Spork also provides onkeypress() and onkeyup, but we will have little use for them.

Key Codes

The following spork code will alert the value of keyCode when different keys are pressed.
function onkeydown(){
	alert(keyCode);
}
You should run this in the spork editor to see some different values. For your convenience, the variables leftArrow, rightArrow, upArrow, and downArrow have been defined so that you can easily check to see if an arrow has been pressed.