Skip to content

Instantly share code, notes, and snippets.

@wolfflow
Last active December 11, 2015 17:09
Show Gist options
  • Save wolfflow/4632974 to your computer and use it in GitHub Desktop.
Save wolfflow/4632974 to your computer and use it in GitHub Desktop.
BaconJS Timed KeySequence listener
arrayEquals = (a,b) -> $(a).not(b).length == 0 && $(b).not(a).length == 0
always = (value) -> (_) -> value
allKeyUps = $(document).asEventStream("keyup")
keyCodeIs = (keyCode) -> (event) -> event.keyCode is keyCode
keyUps = (keyCode) -> allKeyUps.filter keyCodeIs(keyCode)
keyState = (keyCode, value) -> keyUps(keyCode).map(always(value))
arrowKeyState = Bacon.mergeAll([
keyState(38, "UP"),
keyState(40, "DOWN"),
keyState(37, "LEFT"),
keyState(39, "RIGHT")
])
code = ["UP", "RIGHT", "DOWN"]
buf = arrowKeyState.bufferWithTime(200)
buf2 = arrowKeyState.bufferWithCount(code.length)
buf.concat(buf2).onValue((x)-> console.log "HADOUKEN!" if arrayEquals(x,code))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment