Last active
December 11, 2015 17:09
-
-
Save wolfflow/4632974 to your computer and use it in GitHub Desktop.
BaconJS Timed KeySequence listener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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