Git Product home page Git Product logo

cycle-keyboard's Introduction

cycle-keyboard

A keyboard driver for cycle.js. View Demo

npm versionlicense

Usage

Install the package using node package manager

$ npm install [email protected]

Import the driver in your cycle app

import { makeKeyboardDriver } from 'cycle-keyboard'
...
const drivers = {
  ...
  keyboard: makeKeyboardDriver()
}

Subscribe to keyboard-related event streams on the driver

function main({ ..., keyboard }) {
  const keyUp$ = keyboard.ups().map(ev => ev.displayKey + ' was pressed');
}

The driver provides the following xstream streams:

  • ups(key?: number|string), a stream of all keyup events emitted on the document, with an additional displayKey property, optionally filtered by a key code or a key name
  • downs(key?: number|string), a stream of all keydown events emitted on the document, with an additional displayKey property, optionally filtered by a key code or a key name
  • presses(key?: number|string), a stream of all keypress events emitted on the document, with additional displayKey and displayChar properties, optionally filtered by a key code or a key name
  • shift$, a stream of booleans indicating if the shift key is held down, and
  • capsLock$, a stream of booleans indicating if the caps lock is on

cycle-keyboard's People

Contributors

abaco avatar artfuldev avatar nitive avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

nitive abaco

cycle-keyboard's Issues

Add character to keypress

Add a 'character' property to keypress events which cleans up the characters (removes special character presses etc.

Add displayable keys

add an additional field, with display names for the keys pressed, held, or released

Type errors

I try to use it with typescript:

import xs, { Stream } from 'xstream'

const up$: Stream<number> = keyboard.downs('up').mapTo(1)

but got types errors:

error TS2322: Type 'Stream<number>' is not assignable to type 'Stream<number>'.
  Property '_ils' is protected but type 'Stream<T>' is not a class derived from 'Stream<T>'.

Everything works fine without types.

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

Real world example: https://github.com/Nitive/battle-city/blob/b3cb06e/src/index.ts#L15-L18

Filter using keyCode and not name

We might miss alias names if we check the current way:

if(typeof key === 'string')
  return keyUp$.filter(ev => ev.displayKey === key);

We are better off using

if(typeof key === 'string')
  return keyUp$.filter(ev => ev.keyCode === keycode.codes[key]);

Add an isDown$

Given a named key or keycode, this stream will return a boolean indicating whether the key is currently down.

This is a useful stream to have especially for game development, and I have implemented it before in my own Keys Driver. I think it would be a handy method to have in this driver.

Provide original event as stream, don't encapsulate it

The keyboard events javscript api is well known, with event.target, event.keyCode etc. Encapsulating the original event seems like a bad API. Why not add properties displayKey and displayChar to the original event itself and return it?

Ponder the most suitable API

From our gitter conversation:

Sudarsan Balaji @artfuldev 14:53

@Widdershin I initially thought that keyboard.keyUp$.filter(ev => ev.displayKey == 'Ctrl') was a better API (because it conveys intent) than keyboard.keyUp('Ctrl'). Is that at fault?

Nick Johnstone @Widdershin 14:54

do all browsers support displayKey?

Sudarsan Balaji @artfuldev 14:54

It's a property that the driver adds

Nick Johnstone @Widdershin 14:54

ohhh
interesting
I think I would still prefer the latter because it's my most common use case
but I can see the value of your style

Sudarsan Balaji @artfuldev 14:56

@Widdershin I would like to see which people prefer. We could obviously provide both, but it will make the API clobbered with too many functions offering the same. OTOH some APIs provide shortcut functions exposing the same functionality, so it may be acceptable. We should perhaps open up a discussion? Or are you extremely certain that the latter is the way to go?

Raquel Moss @raquelxmoss 14:58

@artfuldev I reckon we should discuss API (and/or copy some of this conversation over to) on Github. It might be nice to have some history on the repo that covers the why of decision making around the API

Sudarsan Balaji @artfuldev 14:58

@raquelxmoss good call.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.