Classdesc

A generic key handler.

Example

Example

// Javascript
new KeyHandler( { trackAll : true } )
.down('enter',function() { console.log('ENTER was hit.'); } )
.press('enter',function() { console.log('ENTER was pressed.'); } )
.up('enter',function() { console.log('ENTER was released.'); } )

.down('e',function() { console.log('e was hit. shift is pressed?',keyHandler.isDown('shift')); } )

.up('windows',function() { console.log('windows was released.'); } )
;

Constructors

  • The constructor.

    Parameters

    • options: {
          element?: typeof globalThis | HTMLElement | Window;
          trackAll?: boolean;
      }
      • Optional element?: typeof globalThis | HTMLElement | Window

        (optional) The HTML element to listen on; if null then 'window' will be used.

      • Optional trackAll?: boolean

        (optional) Set to true if you want to keep track of all keys (keyStatus).

    Returns KeyHandler

    Instance

    Memberof

    KeyHandler

Properties

_keyDownListener: ((e) => void)

Type declaration

    • (e): void
    • Parameters

      • e: KeyboardEvent

      Returns void

_keyPressListener: ((e) => void)

Type declaration

    • (e): void
    • Parameters

      • e: KeyboardEvent

      Returns void

_keyUpListener: ((e) => void)

Type declaration

    • (e): void
    • Parameters

      • e: KeyboardEvent

      Returns void

downListeners: any[] = []
element: typeof globalThis | HTMLElement | Window
keyStates: Record<number, string> = {}
pressListeners: any[] = []
trackAllKeys: boolean
upListeners: any[] = []
KEY_CODES: Record<string, number> = ...

Methods

  • This function should invalidate any installed listeners and invalidate this object. After calling this function the object might not hold valid data any more and should not be used any more.

    Returns void

  • Listen for key down. This function allows chaining.

    Example: new KeyHandler().down('enter',function() {console.log('Enter hit.')});

    Parameters

    • key: string | number

      Any key identifier, key code or one from the KEY_CODES list.

    • listener: XKeyCallback

    Returns KeyHandler

    this

    Name

    down

    Memberof

    KeyHandler

    Instance

  • Private

    Internal function to fire a new keydown event to all listeners. You should not call this function on your own unless you know what you do.

    Parameters

    Returns void

    Name

    fireDownEvent

    Memberof

    KeyHandler

    Instance

  • Private

    Internal function to fire a new keypress event to all listeners. You should not call this function on your own unless you know what you do.

    Parameters

    Returns void

    void

    Name

    firePressEvent

    Memberof

    KeyHandler

    Instance

  • Private

    Internal function to fire a new keyup event to all listeners. You should not call this function on your own unless you know what you do.

    Parameters

    Returns void

    Name

    fireUpEvent

    Memberof

    KeyHandler

    Instance

  • Install the required listeners into the initially passed element.

    By default the listeners are installed into the root element specified on construction (or 'window').

    Returns void

  • Check if a specific key is currently held pressed.

    Parameters

    • key: string | number

      Any key identifier, key code or one from the KEY_CODES list.

    Returns boolean

  • Listen for key press.

    Example: new KeyHandler().press('enter',function() {console.log('Enter pressed.')});

    Parameters

    • key: string | number

      Any key identifier, key code or one from the KEY_CODES list.

    • listener: XKeyCallback

      The callback to be triggered.

    Returns KeyHandler

    this

    Name

    press

    Memberof

    KeyHandler

    Instance