PencilCoder

Mouse Events!

Goals:

  • Make programs responsive to clicks and other mouse-related events
  • Attach listeners to specific page elements or collections of elements

Coding Snippets:

  • coding snippets
  • turtle.click attaches an event listener to the default turtle's HTML page element. That listener executes the event handler rCB each time that sprite is clicked.
  • $('.turtle').click attaches event listeners to all page elements with class attribute turtle, i.e., all sprites.
  • click (alias for window.click) binds the event handler allCB to the page's <html> element. The handler is executed with a click anywhere on the page, i.e., with every click.
  • The event object (e.g., e) provides useful details about the event. Especially useful is the target property, which identifies the specific page element that was clicked.

Math and Computer Concepts:

  • All event listeners are associated with specific page elements. In Pencil Code, the default association is with the window object. For example, the jQuery method keydown is actually an alias for window.keydown. This default works well for most key-related events, because as long as the page is selected, all keystrokes have the potential to fire off events.

Activities:

Save each of the following in a folder called MouseEvents.