Interact

Keyboard

Galacean supports developers to query the current keyboard interaction status at any time, and the interface is very simple to call.

Methods

Method NameDescription
isKeyHeldDownReturns whether the key is being held down
isKeyDownReturns whether the key was pressed in the current frame
isKeyUpReturns whether the key was released in the current frame

Quick Start

Below is a simple example of detecting key states.

class KeyScript extends Script {
  onUpdate() {
    const { inputManager } = this.engine;
    if (inputManager.isKeyHeldDown(Keys.Space)) {
      // 现在还按着空格键
    }
    if (inputManager.isKeyDown(Keys.Space)) {
      // 这帧按下过空格键
    }
    if (inputManager.isKeyUp(Keys.Space)) {
      // 这帧抬起过空格键
    }
  }
}

Practical Example

This time, let's use the spacebar to control Angry Birds.

State Dictionary

Key StateisKeyHeldDownisKeyDownisKeyUp
The key has been held down since the last frametruefalsefalse
The key was pressed in the current frame and not releasedtruetruefalse
The key was released and pressed again in the current frametruetruetrue
The key was pressed and released in the current framefalsetruetrue
The key was released in the current framefalsefalsetrue
The key was not pressed and had no interactionfalsefalsefalse
This situation will not occurtruefalsetrue
This situation will not occurfalsetruefalse

Keys

The keyboard Keys enumerated by Galacean correspond one-to-one with the physical keyboard, following W3C standards, and are compatible with various special keys on different hardware.

Keys Enumeration: https://github.com/galacean/engine/blob/main/packages/core/src/input/enums/Keys.ts

W3C Standard: https://www.w3.org/TR/2017/CR-uievents-code-20170601/

Keyboard Input Design Philosophy: https://github.com/galacean/engine/wiki/Keyboard-Input-design

Was this page helpful?