Inp
State based input library for the browser.
Installation:
Either install with npm or use a CDN such as unpkg (eg. https://unpkg.com/inp/index.js
).
Usage:
import InputReader from 'path/to/inp.js';
let input = InputReader();
const frameRate = 1000/60;
function main() {
if(input.down('a')) console.log('Key "A" pressed!');
if(input.up('MouseLeft')) console.log('Left Mouse Button was released this timestep.');
console.log(input.mouse('x'), input.mouse('y'));
console.log(input.mouse().x, input.mouse('both').y);
input.step();
setTimeout(main, frameRate);
}
main();
API:
InputReader.down(key: string): boolean
Takes in a string that is either a valid event.key
, "MouseLeft"
, "MouseRight"
, "MouseMiddle"
, "MouseBack"
, or "MouseForward"
. Returns a boolean of whether it's down.
InputReader.up(key: string): boolean
Similar InputReader.down
, tells you if a key has been released this timestep, takes the same arguments as InputReader.down
.
InputReader.mouse(which?: string): number | Object
Takes in a string that is either "x"
, "y"
, or "both"
, returns the X position of the mouse, the Y position of the mouse, or {x: <number>, y: <number>}
, respectively. If no argument is given it defaults to "both"
.
InputReader.step(): void
Steps forward once and updates everything. Required to be called before any inputs can be read correctly.
(: