npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

frp-keyboard

v1.4.1

Published

An FRP implementation of Elm.Keyboard in JS

Downloads

10

Readme

frp-keyboard

An FRP implementation of Elm.Keyboard in JS

Example

var Keyboard = require("frp-keyboard");

var keyboard = Keyboard()

keyboard.wasd(function (coords) {
  console.log('x', coords.x, 'y', coords.y)
})

// keys is an Array<Number>
keyboard.keysDown(function (keys) {
  console.log('keys down', keys)
})

Docs

var keyboard = Keyboard()

import { Observ } from 'observ'

type KeyCode := Number
type Direction = 'left' | 'right' | 'up' | 'down' | 'void'
type Coord := {
    x: Number,
    y: Number,
    lastPressed: Direction
}

frp-keyboard := () => {
    arrows: Observ<Coord>,
    wasd: Observ<Coord>,
    ctrl: Observ<Boolean>,
    shift: Observ<Boolean>,
    isDown: (keyCode: KeyCode) => Observ<Boolean>,
    keysDown: Observ<Array<keyCode: KeyCode>>,
    lastPressed: Observ<keyCode: KeyCode>,
    directions: (
        up: KeyCode, down: KeyCode, left: KeyCode, right: KeyCode
    ) => Observ<Coord>
}

Keyboard() returns a keyboard object with a set of observables and functions over observables.

A lot of the values in frp-keyboard are instances of Observ from the 'observ' module

keyboard.arrows

keyboard.arrows is an observable that contains the state of the arrow keys as a tuple { x: Number, y: Number }

You can read from keyboard.arrows by calling it as a function with an optional listener function

var currentX = keyboard.arrows().x

keyboard.arrows(function (tuple) {
  console.log('x', tuple.x)
})

keyboard.arrows also has a .lastPressed property on the returned co-ordinate so you can determine whether a diagonal state of { x: 1, y: 1 } was up, right or right, up

keyboard.wasd

Just like keyboard.arrows, it allows you to read the state of the wasd keys instead.

keyboard.directions(up, down, left, right)

If you want to define your own set of direction keys you can use the directions(). This will return an observable that returns the { x: Number, y: Number } tuple.

var hjkl = keyboard.directions(
  74, // j
  75, // k
  72, // h
  76  // l
)

hjkl(function (coords) {
  console.log('coords.x', coords.x, 'coords.y', coords.y)
})

keyboard.ctrl and keyboard.shift

keyboard.ctrl and keyboard.shift are observable booleans that tell you whether ctrl and shift are pressed down.

var xDown = keyboard.keyDown(Number)

keyDown() returns an observable boolean that tells you whether said key is down.

You pass keyDown a numeric value that matches the keyCode

You can find a list of what the numeric key codes are on MDN

keyboard.keysDown

keysDown is an observable containing an array of key codes.

The keysDown observable represents the lists of keys that are currently being pressed down.

var computed = require('observ/computed')
var Keyboard = require('frp-keyboard')
var keyboard = Keyboard()

var keysDown = keyboard.keysDown

var isEnter = computed([keysDown], function (keysDown) {
  return keysDown.some(function (keyCode) {
    return keyCode === 13 // ENTER
  })
})

keyboard.lastPressed

lastPressed is an observable that contains the last keycode that was pressed by the user.

Installation

npm install frp-keyboard

Tests

npm test

Contributors

  • Raynos

MIT Licenced