API Reference

Class List

pc.Keyboard

A Keyboard device bound to an Element. Allows you to detect the state of the key presses. Note, Keyboard object must be attached to an Element before it can detect any key presses.

var keyboard = new pc.Keyboard(window); // attach keyboard listeners to the window

Summary

Methods

attachAttach the keyboard event handlers to an Element
detachDetach the keyboard event handlers from the element it is attached to.
isPressedReturn true if the key is currently down.
wasPressedReturns true if the key was pressed since the last update.
wasReleasedReturns true if the key was released since the last update.

Events

keydownEvent fired when a key is pressed.
keyupEvent fired when a key is released.

Details

Constructor

Keyboard([element], [options])

Create a new Keyboard object

var keyboard = new pc.Keyboard(window); // attach keyboard listeners to the window

Parameters

elementElementElement to attach Keyboard to. Note that elements like <div> can't accept focus by default. To use keyboard events on an element like this it must have a value of 'tabindex' e.g. tabindex="0". For more details: http://www.w3.org/WAI/GL/WCAG20/WD-WCAG20-TECHS/SCR29.html
optionsObject
options.preventDefaultBooleanCall preventDefault() in key event handlers. This stops the default action of the event occuring. e.g. Ctrl+T will not open a new browser tab
options.stopPropagationBooleanCall stopPropagation() in key event handlers. This stops the event bubbling up the DOM so no parent handlers will be notified of the event

Methods

attach(element)

Attach the keyboard event handlers to an Element

Parameters

elementElementThe element to listen for keyboard events on.

detach()

Detach the keyboard event handlers from the element it is attached to.

isPressed(key)

Return true if the key is currently down.

Parameters

keyNumberThe keyCode of the key to test. See the pc.KEY_* constants.

Returns

Boolean True if the key was pressed, false if not.

wasPressed(key)

Returns true if the key was pressed since the last update.

Parameters

keyNumberThe keyCode of the key to test. See the pc.KEY_* constants.

Returns

Boolean true if the key was pressed.

wasReleased(key)

Returns true if the key was released since the last update.

Parameters

keyNumberThe keyCode of the key to test. See the pc.KEY_* constants.

Returns

Boolean true if the key was pressed.

Events

keydown

Event fired when a key is pressed.

Parameters

eventpc.KeyboardEventThe Keyboard event object. Note, this event is only valid for the current callback.

keyup

Event fired when a key is released.

Parameters

eventpc.KeyboardEventThe Keyboard event object. Note, this event is only valid for the current callback.