Class: Keyboard

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.

Constructor

new Keyboard(elementopt, optionsopt)

Create a new Keyboard object
Parameters:
Name Type Attributes Description
element Element <optional>
Element 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
options Object <optional>
Optional options object.
Properties
Name Type Attributes Description
preventDefault Boolean <optional>
Call preventDefault() in key event handlers. This stops the default action of the event occurring. e.g. Ctrl+T will not open a new browser tab
stopPropagation Boolean <optional>
Call stopPropagation() in key event handlers. This stops the event bubbling up the DOM so no parent handlers will be notified of the event
Source:
Example
var keyboard = new pc.Keyboard(window); // attach keyboard listeners to the window

Methods

attach(element)

Attach the keyboard event handlers to an Element
Parameters:
Name Type Description
element Element The element to listen for keyboard events on.
Source:

detach()

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

isPressed(key) → {Boolean}

Return true if the key is currently down.
Parameters:
Name Type Description
key Number The keyCode of the key to test. See the pc.KEY_* constants.
Source:
Returns:
True if the key was pressed, false if not.
Type
Boolean

(private) toKeyIdentifier(keyCode) → {String}

Convert a key code into a key identifier
Parameters:
Name Type Description
keyCode Number The key code.
Source:
Returns:
The key identifier.
Type
String

(private) update()

Called once per frame to update internal state.
Source:

wasPressed(key) → {Boolean}

Returns true if the key was pressed since the last update.
Parameters:
Name Type Description
key Number The keyCode of the key to test. See the pc.KEY_* constants.
Source:
Returns:
true if the key was pressed.
Type
Boolean

wasReleased(key) → {Boolean}

Returns true if the key was released since the last update.
Parameters:
Name Type Description
key Number The keyCode of the key to test. See the pc.KEY_* constants.
Source:
Returns:
true if the key was pressed.
Type
Boolean

Events

keydown

Event fired when a key is pressed.
Parameters:
Name Type Description
event pc.KeyboardEvent The Keyboard event object. Note, this event is only valid for the current callback.
Source:
Example
var onKeyDown = function (e) {
    if (e.key === pc.KEY_SPACE) {
        // space key pressed
    }
    e.event.preventDefault(); // Use original browser event to prevent browser action.
};
app.keyboard.on("keydown", onKeyDown, this);

keyup

Event fired when a key is released.
Parameters:
Name Type Description
event pc.KeyboardEvent The Keyboard event object. Note, this event is only valid for the current callback.
Source:
Example
var onKeyUp = function (e) {
    if (e.key === pc.KEY_SPACE) {
        // space key released
    }
    e.event.preventDefault(); // Use original browser event to prevent browser action.
};
app.keyboard.on("keyup", onKeyUp, this);