Skip to content

dandre3000/async-keyboard-pointer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

async-keyboard-pointer

Query keyboard and pointer states. Use keyboard and pointer events asynchronously.

Installation

npm i @dandre3000/async-keyboard-pointer

Usage

import {
    asyncKeydown,
    asyncPointerdown,
    getKeyboardButtons,
    getKeyboardState,
    getPointerStateMap,
    getPointerStates
} from '@dandre3000/async-keyboard-pointer'

await Promise.race([
    asyncPointerdown(0),
    asyncKeydown('Enter')
])

let intervalId

const callback = () => {
    const keyboardButtons = getKeyboardButtons('KeyW', 'KeyA', 'KeyS', 'KeyD', 'KeyP', 'KeyQ')
    // or
    const keyboardState = getKeyboardState()

    const pointerState = getPointerStates(0)
    // or for multitouch
    const pointerStateMap = getPointerStateMap()

    if (keyboardButtons[0]) console.log('KeyW')
    if (keyboardButtons[1]) console.log('KeyA')
    if (keyboardButtons[2]) console.log('KeyS')
    if (keyboardButtons[3]) console.log('KeyD')
    if (keyboardButtons[4]) console.log(pointerState)
    if (keyboardButtons[5]) {
        console.log('KeyQ')

        Promise.race([
            asyncPointerdown(0),
            asyncKeydown('Enter')
        ]).then(() => intervalId = requestAnimationFrame(callback))

        return cancelAnimationFrame(intervalId)
    }

    intervalId = requestAnimationFrame(callback)
}

intervalId = requestAnimationFrame(callback)

Exports

interface PointerState {
 button1: boolean
 button2: boolean
 button3: boolean
 button4: boolean
 button5: boolean
 screenX: number
 screenY: number
 clientX: number
 clientY: number
 pageX: number
 pageY: number
 movementX: number
 movementY: number
}

activate: () => void

Add keyboard and pointer event listeners to the root element and enable exported methods.

deactivate: () => void

Remove keyboard and pointer event listeners from the root element, disable exported methods and reject all promises returned by them.

getPointerStates: (...idArray: [number]) => PointerState
getPointerStates: (...idArray: number[]) => PointerState[]

getPointerStateMap: () => Map<number, PointerState>

getKeyboardButtons: (...codeArray: [string]) => boolean
getKeyboardButtons: (...codeArray: string[]) => boolean[]

getKeyboardState: () => Set<string>

asyncPointerenter: (id: PointerEvent["pointerId"], signal?: AbortSignal) => Promise<PointerState>

asyncPointerdown: (id: PointerEvent["pointerId"], signal?: AbortSignal) => Promise<PointerState>

asyncPointermove: (id: PointerEvent["pointerId"], signal?: AbortSignal) => Promise<PointerState>

asyncPointerup: (id: PointerEvent["pointerId"], signal?: AbortSignal) => Promise<PointerState>

asyncClick: (id: PointerEvent["pointerId"], signal?: AbortSignal) => Promise<PointerState>

asyncPointerleave: (id: PointerEvent["pointerId"], signal?: AbortSignal) => Promise<PointerState>

asyncKeydown: (code: KeyboardEvent["code"], signal?: AbortSignal) => Promise<boolean>

asyncKeypress: (code: KeyboardEvent["code"], signal?: AbortSignal) => Promise<boolean>

asyncKeyup: (code: KeyboardEvent["code"], signal?: AbortSignal) => Promise<boolean>

License

MIT

About

Query keyboard and pointer states. Use keyboard and pointer events asynchronously.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors