Keyboard

Keyboard Functions

There are two ways you can get information from the keyboard: Event Listeners and Direct Polling. With an Event Listener, you write a function that gets called automatically when a key is pressed or released. With Direct Polling, you can ask whether a key is pressed whenever you want, which you would typically do during each frame in your update world function.

You can check out the list of key names to find the official names of each keyboard key.

- Keyboard Direct Polling

isKeyPressed

Returns whether a given keyboard key is currently pressed or not.

name
type
default
description

key*

string

-

A string indicating which key to check

Returns

boolean a boolean where true means that key is currently pressed and false means it is not

function updateWorld(world) {
  if (isKeyPressed("space")) {  
    fireRockets = true
  }
}

- Keyboard Event Listeners

onKeyPress

Registers your function that listens for mouse pressed events.

name
type
description

listenerFunction*

function

The listener function that you wrote. Takes one argument: the world.

key*

string

A string indicating which key listen for

Returns

nothing

function moveDown(world) {
  world.myballY = world.myballY + 5
}

function startWorld(world):  {
  onKeyPress(moveDown, "down")
}

onAnyKeyPress

Registers your function that listens for any key pressed events. This function will get called once each time any key is pressed.

name
type
description

listenerFunction*

function

The listener function that you wrote. Takes twos arguments: the world and the key that was pressed

Returns

nothing

function myKeyPressedListener(world, key) {
  if sameKeys(key, "right") {
    world.myballX = world.myballX + 5
  }
  if sameKeys(key, "left") {
    world.myballX = world.myballX - 5
  }
}

function startWorld(world):  {
  onAnyKeyPress(myKeyPressedListener)
}

onKeyRelease

Registers your function that listens for key released events. This function will get called once each time a particular key is released.

name
type
description

listenerFunction*

function

The listener function that you wrote. Takes one argument: the world.

key*

string

A string indicating which key listen for

Returns

nothing

onKeyRelease(spaceBarReleased, "space")

onAnyKeyRelease

Registers your function that listens for any key released events. This function will get called once each time any key is released.

name
type
description

listenerFunction*

function

The listener function that you wrote. Takes twos arguments: the world and the key that was released

Returns

nothing

onAnyKeyRelease(myKeyReleasedListener)

- Keyboard Utility Functions

sameKeys

Checks whether two names for keyboard keys refer to the same key

name
type
default
description

key1*

number

-

The first key to compare

key2*

number

-

The second key to compare

Returns

boolean true means that key is currently pressed and false means it is not

function myKeyPressedFunction(world, key) {
  if sameKeys(key, "right") {
    world.myballX = world.myballX + 5
  }
}

getKeyName

Returns a name for a key

name
type
default
description

key*

number

-

A number that was passed to your key pressed listener or your key released listener.

Returns

string A string that names the key that was pressed or released

function myKeyPressedFunction(world, key) {
  console.log(getKeyName(key))
}

Last updated