PencilCoder

Keyboard Events!

Goals:

  • Make programs interactive by accepting keyboard input
  • Learn about events and event listeners

Coding Snippets:

  • coding snippets
  • Instruct javascript to bind key presses to predefined callback functions using the keydown event binding function.
  • Pass keydown a string specifying which key to listen for (e.g., "a", "up", "space") and a callback specifying the action to take
  • With each key press, JavaScript will invoke the associated callback.

Math and Computer Concepts:

  • Web browsers continuously communicate with the computer operating system to allow pages to react to changes in the state of the computer. Browsers use a special type of object called events to indicate that something of potential interest has occured.
  • Events "fire" when the page loads, when a window is resized or closed, when the mouse is moved, when user clicks a button, when a key is pressed, etc.
  • To make your program react to events, create an event listener for a specific type of event using an event binding function, such as the keydown function.

Activities:

Save each of the following in a folder called KeyboardEvents.