keyWentDown()

Category:Game Lab

Checks if the key specified was pressed.

Some interactive games use the keyboard for the user input to control the game. keyWentDown() generates a single true value when the key is pressed down, no matter how long a key is pressed. Use keyDown() to continually check if the key is pressed.

Examples

function draw() {
  console.log(keyWentDown("left"));
}

Click Counter

Simple click counter using keyWentDown().

// Simple click counter using keyWentDown().
var count=0;
function draw() {
  background("white");
  if (keyWentDown("down")) count=count+1;
  text(count, 200, 200);
}

Syntax

keyWentDown(code)

Parameters

Name Type Required? Description
code String The name of key you want to check. Keys without a letter or number have names like "up", "left", "shift", "tab", "space", etc.

Returns

Boolean true or false.

Tips

  • When testing your games that use keyboard or mouse input make sure you click in the display area before you run, otherwise the Workspace will record your keyboard and mouse actions.

Found a bug in the documentation? Let us know at documentation@code.org