How to detect konami code with Rx.js (reactive extensions for javascript)? -
i want start learning rx.js , i'm looking cool example start ball rolling. how detect konami code rx.js?
i want detect sequence of key press events (up down down left right left right b a) , display image if happens.
here version:
<html> <head> <script type="text/javascript" src="jquery-1.4.4.min.js"></script> <script type="text/javascript" src="rx.js"></script> <script type="text/javascript" src="rx.jquery.js"></script> </head> <body> <p id="result"></p> <script type="text/javascript"> $(function() { var konami = $(document).toobservable("keyup").select(function(e) { return e.keycode }).skipwhile(function(k) { return (k != 38) }).bufferwithcount( 10 ).where(function(ks) { return ks.length == 10 && ks[0] == 38 && ks[1] == 38 && ks[2] == 40 && ks[3] == 40 && ks[4] == 37 && ks[5] == 39 && ks[6] == 37 && ks[7] == 39 && ks[8] == 66 && ks[9] == 65 }) var konamisub = konami.subscribe(function(e) { $("#result").text("konami!") $("#result").fadein().fadeout() }) }) </script> </body> </html>
i convert stream of keyup events stream of keycodes select
, ignoring keypresses until user press (keycode 38) skipwhile
, collecting 10 keystrokes bufferwithcount
, checking keystrokes where
.
i tried using bufferwithtime, tends cut in middle of keystrokes.
if can suggest improvements, love hear them.
Comments
Post a Comment