gameify. KeyboardEventManager

new KeyboardEventManager(scope)

Manages keyboard events. One is created automatically for every screen, you can access it as shown in the example.

Parameters:
NameTypeDescription
scopeHTMLElement

What parts of the screen the KeyboardEventManager looks for.

Example
// make a new screen and scene
let myScreen = new gameify.Screen(document.querySelector("#my-canvas"), 600, 400);
let myScene = new gameify.Scene(myScreen);
myScene.onUpdate(() => {
    if (myScreen.keyboard.keyIsPressed("Escape")) {
        myScreen.setScene(pause_menu);
    }
});

Methods

(package) destruct()

Destructs the event manager (clears events)

forceClearPressedKeys()

Force clear pressed keys

keyIsPressed(key) → {Boolean}

Check if a key is currently pressed down

Parameters:
NameTypeDescription
keyString

What key do you want to check

Returns:

if the key is pressed down

Type: 
Boolean
Example
// see if the right arrow is pressed
if (myGame.keyboard.keyIsPressed("Right")) {
    // set the player motion
    player.velocity.x = 5;
}

keyWasJustPressed(key) → {Boolean}

Check if a key was just pressed and let up

Parameters:
NameTypeDescription
keyString

What key do you want to check

Returns:

if the key was just pressed

Type: 
Boolean
Example
// See if the player pressed the Escape key.
if (myScreen.keyboard.keyWasJustPressed("Escape")) {
    myScreen.setScene(mainMenu);
}

(package) makeKeyNicer(key) → {String}

Makes pressed keys nicer, for simpler queries (KeyH --> H)

Parameters:
NameTypeDescription
keyString

The key to make nicer

Returns:

The input key, with extra text stripped away.

Type: 
String

onFirstFocus(callback)

Run a callback when the game is first focused (Useful for starting audio, etc)

Parameters:
NameTypeDescription
callbackfunction

The callback to run

setCaptureScope(scope)

Changes the scope that the KeyboardInputManager looks at

Parameters:
NameTypeDescription
scopeHTMLElement

What parts of the screen the KeyboardEventManager looks for.

setJustPressedTimeout(timeout)

Sets how long before "just pressed" keys are removed from the just pressed list. By default, keys are removed from the list after one frame.

Parameters:
NameTypeDescription
timeoutNumber

How many frames/updates keys should be considered "just pressed"

(package) setup()

Sets up the event manager.