Skip to content

Keyboard Input design

AZhan edited this page Mar 4, 2022 · 4 revisions

When implementing the input system, we made compatibility for all inputs, this keyboard event can be implemented very simply. Input system design image

API Design

class Script
{
    .......
   
    /**
     * Called when the keyboard is pressed.
     */
    onKeyDown(key:KeyEvent): void {}
  
    /**
     * Called when the keyboard is up.
     */
    onKeyUp(key:KeyEvent): void {}
   
   ......
}

/**
 * Input Manager.
 */
export class InputManager {

   // Whether the button is being held down.
   getKey(key:KeyCode | string): Boolean {}

   // Whether the current frame triggers a key press.
   getKeyDown(key:KeyCode | string): Boolean {}

   // Whether the current frame triggers a key Up.
   getKeyUp(key:KeyCode | string): Boolean {}
}

/**
 * Description of key information.
 */
export class Key {
    key: string;
    code: string;
    codeNumber: KeyCode;
}